zynq_qspi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * (C) Copyright 2013 Xilinx, Inc.
  3. * (C) Copyright 2015 Jagan Teki <jteki@openedev.com>
  4. *
  5. * Xilinx Zynq Quad-SPI(QSPI) controller driver (master mode only)
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <malloc.h>
  12. #include <spi.h>
  13. #include <asm/io.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /* zynq qspi register bit masks ZYNQ_QSPI_<REG>_<BIT>_MASK */
  16. #define ZYNQ_QSPI_CR_IFMODE_MASK BIT(31) /* Flash intrface mode*/
  17. #define ZYNQ_QSPI_CR_MSA_MASK BIT(15) /* Manual start enb */
  18. #define ZYNQ_QSPI_CR_MCS_MASK BIT(14) /* Manual chip select */
  19. #define ZYNQ_QSPI_CR_PCS_MASK BIT(10) /* Peri chip select */
  20. #define ZYNQ_QSPI_CR_FW_MASK GENMASK(7, 6) /* FIFO width */
  21. #define ZYNQ_QSPI_CR_SS_MASK GENMASK(13, 10) /* Slave Select */
  22. #define ZYNQ_QSPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
  23. #define ZYNQ_QSPI_CR_CPHA_MASK BIT(2) /* Clock phase */
  24. #define ZYNQ_QSPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
  25. #define ZYNQ_QSPI_CR_MSTREN_MASK BIT(0) /* Mode select */
  26. #define ZYNQ_QSPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
  27. #define ZYNQ_QSPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
  28. #define ZYNQ_QSPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
  29. #define ZYNQ_QSPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
  30. #define ZYNQ_QSPI_LQSPICFG_LQMODE_MASK BIT(31) /* Linear QSPI Mode */
  31. /* zynq qspi Transmit Data Register */
  32. #define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst */
  33. #define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst */
  34. #define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst */
  35. #define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst */
  36. #define ZYNQ_QSPI_TXFIFO_THRESHOLD 1 /* Tx FIFO threshold level*/
  37. #define ZYNQ_QSPI_RXFIFO_THRESHOLD 32 /* Rx FIFO threshold level */
  38. #define ZYNQ_QSPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
  39. #define ZYNQ_QSPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
  40. #define ZYNQ_QSPI_CR_SS_SHIFT 10 /* Slave select shift */
  41. #define ZYNQ_QSPI_FIFO_DEPTH 63
  42. #ifndef CONFIG_SYS_ZYNQ_QSPI_WAIT
  43. #define CONFIG_SYS_ZYNQ_QSPI_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
  44. #endif
  45. /* zynq qspi register set */
  46. struct zynq_qspi_regs {
  47. u32 cr; /* 0x00 */
  48. u32 isr; /* 0x04 */
  49. u32 ier; /* 0x08 */
  50. u32 idr; /* 0x0C */
  51. u32 imr; /* 0x10 */
  52. u32 enr; /* 0x14 */
  53. u32 dr; /* 0x18 */
  54. u32 txd0r; /* 0x1C */
  55. u32 drxr; /* 0x20 */
  56. u32 sicr; /* 0x24 */
  57. u32 txftr; /* 0x28 */
  58. u32 rxftr; /* 0x2C */
  59. u32 gpior; /* 0x30 */
  60. u32 reserved0[19];
  61. u32 txd1r; /* 0x80 */
  62. u32 txd2r; /* 0x84 */
  63. u32 txd3r; /* 0x88 */
  64. u32 reserved1[5];
  65. u32 lqspicfg; /* 0xA0 */
  66. u32 lqspists; /* 0xA4 */
  67. };
  68. /* zynq qspi platform data */
  69. struct zynq_qspi_platdata {
  70. struct zynq_qspi_regs *regs;
  71. u32 frequency; /* input frequency */
  72. u32 speed_hz;
  73. };
  74. /* zynq qspi priv */
  75. struct zynq_qspi_priv {
  76. struct zynq_qspi_regs *regs;
  77. u8 cs;
  78. u8 mode;
  79. u8 fifo_depth;
  80. u32 freq; /* required frequency */
  81. const void *tx_buf;
  82. void *rx_buf;
  83. unsigned len;
  84. int bytes_to_transfer;
  85. int bytes_to_receive;
  86. unsigned int is_inst;
  87. unsigned cs_change:1;
  88. };
  89. static int zynq_qspi_ofdata_to_platdata(struct udevice *bus)
  90. {
  91. struct zynq_qspi_platdata *plat = bus->platdata;
  92. const void *blob = gd->fdt_blob;
  93. int node = bus->of_offset;
  94. plat->regs = (struct zynq_qspi_regs *)fdtdec_get_addr(blob,
  95. node, "reg");
  96. /* FIXME: Use 166MHz as a suitable default */
  97. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  98. 166666666);
  99. plat->speed_hz = plat->frequency / 2;
  100. debug("%s: regs=%p max-frequency=%d\n", __func__,
  101. plat->regs, plat->frequency);
  102. return 0;
  103. }
  104. static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv)
  105. {
  106. struct zynq_qspi_regs *regs = priv->regs;
  107. u32 confr;
  108. /* Disable QSPI */
  109. writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
  110. /* Disable Interrupts */
  111. writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->idr);
  112. /* Clear the TX and RX threshold reg */
  113. writel(ZYNQ_QSPI_TXFIFO_THRESHOLD, &regs->txftr);
  114. writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, &regs->rxftr);
  115. /* Clear the RX FIFO */
  116. while (readl(&regs->isr) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)
  117. readl(&regs->drxr);
  118. /* Clear Interrupts */
  119. writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->isr);
  120. /* Manual slave select and Auto start */
  121. confr = readl(&regs->cr);
  122. confr &= ~ZYNQ_QSPI_CR_MSA_MASK;
  123. confr |= ZYNQ_QSPI_CR_IFMODE_MASK | ZYNQ_QSPI_CR_MCS_MASK |
  124. ZYNQ_QSPI_CR_PCS_MASK | ZYNQ_QSPI_CR_FW_MASK |
  125. ZYNQ_QSPI_CR_MSTREN_MASK;
  126. writel(confr, &regs->cr);
  127. /* Disable the LQSPI feature */
  128. confr = readl(&regs->lqspicfg);
  129. confr &= ~ZYNQ_QSPI_LQSPICFG_LQMODE_MASK;
  130. writel(confr, &regs->lqspicfg);
  131. /* Enable SPI */
  132. writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
  133. }
  134. static int zynq_qspi_probe(struct udevice *bus)
  135. {
  136. struct zynq_qspi_platdata *plat = dev_get_platdata(bus);
  137. struct zynq_qspi_priv *priv = dev_get_priv(bus);
  138. priv->regs = plat->regs;
  139. priv->fifo_depth = ZYNQ_QSPI_FIFO_DEPTH;
  140. /* init the zynq spi hw */
  141. zynq_qspi_init_hw(priv);
  142. return 0;
  143. }
  144. /*
  145. * zynq_qspi_read_data - Copy data to RX buffer
  146. * @zqspi: Pointer to the zynq_qspi structure
  147. * @data: The 32 bit variable where data is stored
  148. * @size: Number of bytes to be copied from data to RX buffer
  149. */
  150. static void zynq_qspi_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size)
  151. {
  152. u8 byte3;
  153. debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__ ,
  154. data, (unsigned)(priv->rx_buf), size);
  155. if (priv->rx_buf) {
  156. switch (size) {
  157. case 1:
  158. *((u8 *)priv->rx_buf) = data;
  159. priv->rx_buf += 1;
  160. break;
  161. case 2:
  162. *((u16 *)priv->rx_buf) = data;
  163. priv->rx_buf += 2;
  164. break;
  165. case 3:
  166. *((u16 *)priv->rx_buf) = data;
  167. priv->rx_buf += 2;
  168. byte3 = (u8)(data >> 16);
  169. *((u8 *)priv->rx_buf) = byte3;
  170. priv->rx_buf += 1;
  171. break;
  172. case 4:
  173. /* Can not assume word aligned buffer */
  174. memcpy(priv->rx_buf, &data, size);
  175. priv->rx_buf += 4;
  176. break;
  177. default:
  178. /* This will never execute */
  179. break;
  180. }
  181. }
  182. priv->bytes_to_receive -= size;
  183. if (priv->bytes_to_receive < 0)
  184. priv->bytes_to_receive = 0;
  185. }
  186. /*
  187. * zynq_qspi_write_data - Copy data from TX buffer
  188. * @zqspi: Pointer to the zynq_qspi structure
  189. * @data: Pointer to the 32 bit variable where data is to be copied
  190. * @size: Number of bytes to be copied from TX buffer to data
  191. */
  192. static void zynq_qspi_write_data(struct zynq_qspi_priv *priv,
  193. u32 *data, u8 size)
  194. {
  195. if (priv->tx_buf) {
  196. switch (size) {
  197. case 1:
  198. *data = *((u8 *)priv->tx_buf);
  199. priv->tx_buf += 1;
  200. *data |= 0xFFFFFF00;
  201. break;
  202. case 2:
  203. *data = *((u16 *)priv->tx_buf);
  204. priv->tx_buf += 2;
  205. *data |= 0xFFFF0000;
  206. break;
  207. case 3:
  208. *data = *((u16 *)priv->tx_buf);
  209. priv->tx_buf += 2;
  210. *data |= (*((u8 *)priv->tx_buf) << 16);
  211. priv->tx_buf += 1;
  212. *data |= 0xFF000000;
  213. break;
  214. case 4:
  215. /* Can not assume word aligned buffer */
  216. memcpy(data, priv->tx_buf, size);
  217. priv->tx_buf += 4;
  218. break;
  219. default:
  220. /* This will never execute */
  221. break;
  222. }
  223. } else {
  224. *data = 0;
  225. }
  226. debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__,
  227. *data, (u32)priv->tx_buf, size);
  228. priv->bytes_to_transfer -= size;
  229. if (priv->bytes_to_transfer < 0)
  230. priv->bytes_to_transfer = 0;
  231. }
  232. static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on)
  233. {
  234. u32 confr;
  235. struct zynq_qspi_regs *regs = priv->regs;
  236. confr = readl(&regs->cr);
  237. if (is_on) {
  238. /* Select the slave */
  239. confr &= ~ZYNQ_QSPI_CR_SS_MASK;
  240. confr |= (~(1 << priv->cs) << ZYNQ_QSPI_CR_SS_SHIFT) &
  241. ZYNQ_QSPI_CR_SS_MASK;
  242. } else
  243. /* Deselect the slave */
  244. confr |= ZYNQ_QSPI_CR_SS_MASK;
  245. writel(confr, &regs->cr);
  246. }
  247. /*
  248. * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
  249. * @zqspi: Pointer to the zynq_qspi structure
  250. */
  251. static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size)
  252. {
  253. u32 data = 0;
  254. u32 fifocount = 0;
  255. unsigned len, offset;
  256. struct zynq_qspi_regs *regs = priv->regs;
  257. static const unsigned offsets[4] = {
  258. ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET,
  259. ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET };
  260. while ((fifocount < size) &&
  261. (priv->bytes_to_transfer > 0)) {
  262. if (priv->bytes_to_transfer >= 4) {
  263. if (priv->tx_buf) {
  264. memcpy(&data, priv->tx_buf, 4);
  265. priv->tx_buf += 4;
  266. } else {
  267. data = 0;
  268. }
  269. writel(data, &regs->txd0r);
  270. priv->bytes_to_transfer -= 4;
  271. fifocount++;
  272. } else {
  273. /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */
  274. if (!(readl(&regs->isr)
  275. & ZYNQ_QSPI_IXR_TXOW_MASK) &&
  276. !priv->rx_buf)
  277. return;
  278. len = priv->bytes_to_transfer;
  279. zynq_qspi_write_data(priv, &data, len);
  280. offset = (priv->rx_buf) ? offsets[0] : offsets[len];
  281. writel(data, &regs->cr + (offset / 4));
  282. }
  283. }
  284. }
  285. /*
  286. * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller
  287. * @zqspi: Pointer to the zynq_qspi structure
  288. *
  289. * This function handles TX empty and Mode Fault interrupts only.
  290. * On TX empty interrupt this function reads the received data from RX FIFO and
  291. * fills the TX FIFO if there is any data remaining to be transferred.
  292. * On Mode Fault interrupt this function indicates that transfer is completed,
  293. * the SPI subsystem will identify the error as the remaining bytes to be
  294. * transferred is non-zero.
  295. *
  296. * returns: 0 for poll timeout
  297. * 1 transfer operation complete
  298. */
  299. static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv)
  300. {
  301. struct zynq_qspi_regs *regs = priv->regs;
  302. u32 rxindex = 0;
  303. u32 rxcount;
  304. u32 status, timeout;
  305. /* Poll until any of the interrupt status bits are set */
  306. timeout = get_timer(0);
  307. do {
  308. status = readl(&regs->isr);
  309. } while ((status == 0) &&
  310. (get_timer(timeout) < CONFIG_SYS_ZYNQ_QSPI_WAIT));
  311. if (status == 0) {
  312. printf("zynq_qspi_irq_poll: Timeout!\n");
  313. return -ETIMEDOUT;
  314. }
  315. writel(status, &regs->isr);
  316. /* Disable all interrupts */
  317. writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->idr);
  318. if ((status & ZYNQ_QSPI_IXR_TXOW_MASK) ||
  319. (status & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)) {
  320. /*
  321. * This bit is set when Tx FIFO has < THRESHOLD entries. We have
  322. * the THRESHOLD value set to 1, so this bit indicates Tx FIFO
  323. * is empty
  324. */
  325. rxcount = priv->bytes_to_receive - priv->bytes_to_transfer;
  326. rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4);
  327. while ((rxindex < rxcount) &&
  328. (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) {
  329. /* Read out the data from the RX FIFO */
  330. u32 data;
  331. data = readl(&regs->drxr);
  332. if (priv->bytes_to_receive >= 4) {
  333. if (priv->rx_buf) {
  334. memcpy(priv->rx_buf, &data, 4);
  335. priv->rx_buf += 4;
  336. }
  337. priv->bytes_to_receive -= 4;
  338. } else {
  339. zynq_qspi_read_data(priv, data,
  340. priv->bytes_to_receive);
  341. }
  342. rxindex++;
  343. }
  344. if (priv->bytes_to_transfer) {
  345. /* There is more data to send */
  346. zynq_qspi_fill_tx_fifo(priv,
  347. ZYNQ_QSPI_RXFIFO_THRESHOLD);
  348. writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->ier);
  349. } else {
  350. /*
  351. * If transfer and receive is completed then only send
  352. * complete signal
  353. */
  354. if (!priv->bytes_to_receive) {
  355. /* return operation complete */
  356. writel(ZYNQ_QSPI_IXR_ALL_MASK,
  357. &regs->idr);
  358. return 1;
  359. }
  360. }
  361. }
  362. return 0;
  363. }
  364. /*
  365. * zynq_qspi_start_transfer - Initiates the QSPI transfer
  366. * @qspi: Pointer to the spi_device structure
  367. * @transfer: Pointer to the spi_transfer structure which provide information
  368. * about next transfer parameters
  369. *
  370. * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
  371. * transfer to be completed.
  372. *
  373. * returns: Number of bytes transferred in the last transfer
  374. */
  375. static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv)
  376. {
  377. u32 data = 0;
  378. struct zynq_qspi_regs *regs = priv->regs;
  379. debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__,
  380. (u32)priv, (u32)priv, priv->len);
  381. priv->bytes_to_transfer = priv->len;
  382. priv->bytes_to_receive = priv->len;
  383. if (priv->len < 4)
  384. zynq_qspi_fill_tx_fifo(priv, priv->len);
  385. else
  386. zynq_qspi_fill_tx_fifo(priv, priv->fifo_depth);
  387. writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->ier);
  388. /* wait for completion */
  389. do {
  390. data = zynq_qspi_irq_poll(priv);
  391. } while (data == 0);
  392. return (priv->len) - (priv->bytes_to_transfer);
  393. }
  394. static int zynq_qspi_transfer(struct zynq_qspi_priv *priv)
  395. {
  396. unsigned cs_change = 1;
  397. int status = 0;
  398. while (1) {
  399. /* Select the chip if required */
  400. if (cs_change)
  401. zynq_qspi_chipselect(priv, 1);
  402. cs_change = priv->cs_change;
  403. if (!priv->tx_buf && !priv->rx_buf && priv->len) {
  404. status = -1;
  405. break;
  406. }
  407. /* Request the transfer */
  408. if (priv->len) {
  409. status = zynq_qspi_start_transfer(priv);
  410. priv->is_inst = 0;
  411. }
  412. if (status != priv->len) {
  413. if (status > 0)
  414. status = -EMSGSIZE;
  415. debug("zynq_qspi_transfer:%d len:%d\n",
  416. status, priv->len);
  417. break;
  418. }
  419. status = 0;
  420. if (cs_change)
  421. /* Deselect the chip */
  422. zynq_qspi_chipselect(priv, 0);
  423. break;
  424. }
  425. return 0;
  426. }
  427. static int zynq_qspi_claim_bus(struct udevice *dev)
  428. {
  429. struct udevice *bus = dev->parent;
  430. struct zynq_qspi_priv *priv = dev_get_priv(bus);
  431. struct zynq_qspi_regs *regs = priv->regs;
  432. writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
  433. return 0;
  434. }
  435. static int zynq_qspi_release_bus(struct udevice *dev)
  436. {
  437. struct udevice *bus = dev->parent;
  438. struct zynq_qspi_priv *priv = dev_get_priv(bus);
  439. struct zynq_qspi_regs *regs = priv->regs;
  440. writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
  441. return 0;
  442. }
  443. static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen,
  444. const void *dout, void *din, unsigned long flags)
  445. {
  446. struct udevice *bus = dev->parent;
  447. struct zynq_qspi_priv *priv = dev_get_priv(bus);
  448. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  449. priv->cs = slave_plat->cs;
  450. priv->tx_buf = dout;
  451. priv->rx_buf = din;
  452. priv->len = bitlen / 8;
  453. debug("zynq_qspi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
  454. bus->seq, slave_plat->cs, bitlen, priv->len, flags);
  455. /*
  456. * Festering sore.
  457. * Assume that the beginning of a transfer with bits to
  458. * transmit must contain a device command.
  459. */
  460. if (dout && flags & SPI_XFER_BEGIN)
  461. priv->is_inst = 1;
  462. else
  463. priv->is_inst = 0;
  464. if (flags & SPI_XFER_END)
  465. priv->cs_change = 1;
  466. else
  467. priv->cs_change = 0;
  468. zynq_qspi_transfer(priv);
  469. return 0;
  470. }
  471. static int zynq_qspi_set_speed(struct udevice *bus, uint speed)
  472. {
  473. struct zynq_qspi_platdata *plat = bus->platdata;
  474. struct zynq_qspi_priv *priv = dev_get_priv(bus);
  475. struct zynq_qspi_regs *regs = priv->regs;
  476. uint32_t confr;
  477. u8 baud_rate_val = 0;
  478. if (speed > plat->frequency)
  479. speed = plat->frequency;
  480. /* Set the clock frequency */
  481. confr = readl(&regs->cr);
  482. if (speed == 0) {
  483. /* Set baudrate x8, if the freq is 0 */
  484. baud_rate_val = 0x2;
  485. } else if (plat->speed_hz != speed) {
  486. while ((baud_rate_val < ZYNQ_QSPI_CR_BAUD_MAX) &&
  487. ((plat->frequency /
  488. (2 << baud_rate_val)) > speed))
  489. baud_rate_val++;
  490. plat->speed_hz = speed / (2 << baud_rate_val);
  491. }
  492. confr &= ~ZYNQ_QSPI_CR_BAUD_MASK;
  493. confr |= (baud_rate_val << ZYNQ_QSPI_CR_BAUD_SHIFT);
  494. writel(confr, &regs->cr);
  495. priv->freq = speed;
  496. debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
  497. return 0;
  498. }
  499. static int zynq_qspi_set_mode(struct udevice *bus, uint mode)
  500. {
  501. struct zynq_qspi_priv *priv = dev_get_priv(bus);
  502. struct zynq_qspi_regs *regs = priv->regs;
  503. uint32_t confr;
  504. /* Set the SPI Clock phase and polarities */
  505. confr = readl(&regs->cr);
  506. confr &= ~(ZYNQ_QSPI_CR_CPHA_MASK | ZYNQ_QSPI_CR_CPOL_MASK);
  507. if (mode & SPI_CPHA)
  508. confr |= ZYNQ_QSPI_CR_CPHA_MASK;
  509. if (mode & SPI_CPOL)
  510. confr |= ZYNQ_QSPI_CR_CPOL_MASK;
  511. writel(confr, &regs->cr);
  512. priv->mode = mode;
  513. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  514. return 0;
  515. }
  516. static const struct dm_spi_ops zynq_qspi_ops = {
  517. .claim_bus = zynq_qspi_claim_bus,
  518. .release_bus = zynq_qspi_release_bus,
  519. .xfer = zynq_qspi_xfer,
  520. .set_speed = zynq_qspi_set_speed,
  521. .set_mode = zynq_qspi_set_mode,
  522. };
  523. static const struct udevice_id zynq_qspi_ids[] = {
  524. { .compatible = "xlnx,zynq-qspi-1.0" },
  525. { }
  526. };
  527. U_BOOT_DRIVER(zynq_qspi) = {
  528. .name = "zynq_qspi",
  529. .id = UCLASS_SPI,
  530. .of_match = zynq_qspi_ids,
  531. .ops = &zynq_qspi_ops,
  532. .ofdata_to_platdata = zynq_qspi_ofdata_to_platdata,
  533. .platdata_auto_alloc_size = sizeof(struct zynq_qspi_platdata),
  534. .priv_auto_alloc_size = sizeof(struct zynq_qspi_priv),
  535. .probe = zynq_qspi_probe,
  536. };