cf_qspi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Freescale Coldfire Queued SPI driver
  3. *
  4. * NOTE:
  5. * This driver is written to transfer 8 bit at-a-time and uses the dedicated
  6. * SPI slave select pins as bit-banged GPIO to work with spi_flash subsystem.
  7. *
  8. *
  9. * Copyright (C) 2011 Ruggedcom, Inc.
  10. * Richard Retanubun (richardretanubun@freescale.com)
  11. *
  12. * See file CREDITS for list of people who contributed to this project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <malloc.h>
  31. #include <spi.h>
  32. #include <asm/immap.h>
  33. #include <asm/io.h>
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #define clamp(x, low, high) (min(max(low, x), high))
  36. #define to_cf_qspi_slave(s) container_of(s, struct cf_qspi_slave, s)
  37. struct cf_qspi_slave {
  38. struct spi_slave slave; /* Specific bus:cs ID for each device */
  39. qspi_t *regs; /* Pointer to SPI controller registers */
  40. u16 qmr; /* QMR: Queued Mode Register */
  41. u16 qwr; /* QWR: Queued Wrap Register */
  42. u16 qcr; /* QCR: Queued Command Ram */
  43. };
  44. /* Register write wrapper functions */
  45. static void write_qmr(volatile qspi_t *qspi, u16 val) { qspi->mr = val; }
  46. static void write_qdlyr(volatile qspi_t *qspi, u16 val) { qspi->dlyr = val; }
  47. static void write_qwr(volatile qspi_t *qspi, u16 val) { qspi->wr = val; }
  48. static void write_qir(volatile qspi_t *qspi, u16 val) { qspi->ir = val; }
  49. static void write_qar(volatile qspi_t *qspi, u16 val) { qspi->ar = val; }
  50. static void write_qdr(volatile qspi_t *qspi, u16 val) { qspi->dr = val; }
  51. /* Register read wrapper functions */
  52. static u16 read_qdlyr(volatile qspi_t *qspi) { return qspi->dlyr; }
  53. static u16 read_qwr(volatile qspi_t *qspi) { return qspi->wr; }
  54. static u16 read_qir(volatile qspi_t *qspi) { return qspi->ir; }
  55. static u16 read_qdr(volatile qspi_t *qspi) { return qspi->dr; }
  56. /* These call points may be different for each ColdFire CPU */
  57. extern void cfspi_port_conf(void);
  58. static void cfspi_cs_activate(uint bus, uint cs, uint cs_active_high);
  59. static void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high);
  60. int spi_claim_bus(struct spi_slave *slave)
  61. {
  62. return 0;
  63. }
  64. void spi_release_bus(struct spi_slave *slave)
  65. {
  66. }
  67. __attribute__((weak))
  68. void spi_init(void)
  69. {
  70. cfspi_port_conf();
  71. }
  72. __attribute__((weak))
  73. void spi_cs_activate(struct spi_slave *slave)
  74. {
  75. struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
  76. cfspi_cs_activate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV));
  77. }
  78. __attribute__((weak))
  79. void spi_cs_deactivate(struct spi_slave *slave)
  80. {
  81. struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
  82. cfspi_cs_deactivate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV));
  83. }
  84. __attribute__((weak))
  85. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  86. {
  87. /* Only 1 bus and 4 chipselect per controller */
  88. if (bus == 0 && (cs >= 0 && cs < 4))
  89. return 1;
  90. else
  91. return 0;
  92. }
  93. void spi_free_slave(struct spi_slave *slave)
  94. {
  95. struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
  96. free(dev);
  97. }
  98. /* Translate information given by spi_setup_slave to members of cf_qspi_slave */
  99. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  100. unsigned int max_hz, unsigned int mode)
  101. {
  102. struct cf_qspi_slave *dev = NULL;
  103. if (!spi_cs_is_valid(bus, cs))
  104. return NULL;
  105. dev = spi_alloc_slave(struct cf_qspi_slave, bus, cs);
  106. if (!dev)
  107. return NULL;
  108. /* Initialize to known value */
  109. dev->regs = (qspi_t *)MMAP_QSPI;
  110. dev->qmr = 0;
  111. dev->qwr = 0;
  112. dev->qcr = 0;
  113. /* Map max_hz to QMR[BAUD] */
  114. if (max_hz == 0) /* Go as fast as possible */
  115. dev->qmr = 2u;
  116. else /* Get the closest baud rate */
  117. dev->qmr = clamp(((gd->bus_clk >> 2) + max_hz - 1)/max_hz,
  118. 2u, 255u);
  119. /* Map mode to QMR[CPOL] and QMR[CPHA] */
  120. if (mode & SPI_CPOL)
  121. dev->qmr |= QSPI_QMR_CPOL;
  122. if (mode & SPI_CPHA)
  123. dev->qmr |= QSPI_QMR_CPHA;
  124. /* Hardcode bit length to 8 bit per transter */
  125. dev->qmr |= QSPI_QMR_BITS_8;
  126. /* Set QMR[MSTR] to enable QSPI as master */
  127. dev->qmr |= QSPI_QMR_MSTR;
  128. /*
  129. * Set QCR and QWR to default values for spi flash operation.
  130. * If more custom QCR and QRW are needed, overload mode variable
  131. */
  132. dev->qcr = (QSPI_QDR_CONT | QSPI_QDR_BITSE);
  133. if (!(mode & SPI_CS_HIGH))
  134. dev->qwr |= QSPI_QWR_CSIV;
  135. return &dev->slave;
  136. }
  137. /* Transfer 8 bit at a time */
  138. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  139. void *din, unsigned long flags)
  140. {
  141. struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
  142. volatile qspi_t *qspi = dev->regs;
  143. u8 *txbuf = (u8 *)dout;
  144. u8 *rxbuf = (u8 *)din;
  145. u32 count = ((bitlen / 8) + (bitlen % 8 ? 1 : 0));
  146. u32 n, i = 0;
  147. /* Sanitize arguments */
  148. if (slave == NULL) {
  149. printf("%s: NULL slave ptr\n", __func__);
  150. return -1;
  151. }
  152. if (flags & SPI_XFER_BEGIN)
  153. spi_cs_activate(slave);
  154. /* There is something to send, lets process it. spi_xfer is also called
  155. * just to toggle chip select, so bitlen of 0 is valid */
  156. if (count > 0) {
  157. /*
  158. * NOTE: Since chip select is driven as a bit-bang-ed GPIO
  159. * using spi_cs_activate() and spi_cs_deactivate(),
  160. * the chip select settings inside the controller
  161. * (i.e. QCR[CONT] and QWR[CSIV]) are moot. The bits are set to
  162. * keep the controller settings consistent with the actual
  163. * operation of the bus.
  164. */
  165. /* Write the slave device's settings for the controller.*/
  166. write_qmr(qspi, dev->qmr);
  167. write_qwr(qspi, dev->qwr);
  168. /* Limit transfer to 16 at a time */
  169. n = min(count, 16u);
  170. do {
  171. /* Setup queue end point */
  172. write_qwr(qspi, ((read_qwr(qspi) & QSPI_QWR_ENDQP_MASK)
  173. | QSPI_QWR_ENDQP((n-1))));
  174. /* Write Command RAM */
  175. write_qar(qspi, QSPI_QAR_CMD);
  176. for (i = 0; i < n; ++i)
  177. write_qdr(qspi, dev->qcr);
  178. /* Write TxBuf, if none given, fill with ZEROes */
  179. write_qar(qspi, QSPI_QAR_TRANS);
  180. if (txbuf) {
  181. for (i = 0; i < n; ++i)
  182. write_qdr(qspi, *txbuf++);
  183. } else {
  184. for (i = 0; i < n; ++i)
  185. write_qdr(qspi, 0);
  186. }
  187. /* Clear QIR[SPIF] by writing a 1 to it */
  188. write_qir(qspi, read_qir(qspi) | QSPI_QIR_SPIF);
  189. /* Set QDLYR[SPE] to start sending */
  190. write_qdlyr(qspi, read_qdlyr(qspi) | QSPI_QDLYR_SPE);
  191. /* Poll QIR[SPIF] for transfer completion */
  192. while ((read_qir(qspi) & QSPI_QIR_SPIF) != 1)
  193. udelay(1);
  194. /* If given read RxBuf, load data to it */
  195. if (rxbuf) {
  196. write_qar(qspi, QSPI_QAR_RECV);
  197. for (i = 0; i < n; ++i)
  198. *rxbuf++ = read_qdr(qspi);
  199. }
  200. /* Decrement count */
  201. count -= n;
  202. } while (count);
  203. }
  204. if (flags & SPI_XFER_END)
  205. spi_cs_deactivate(slave);
  206. return 0;
  207. }
  208. /* Each MCF CPU may have different pin assignments for chip selects. */
  209. #if defined(CONFIG_M5271)
  210. /* Assert chip select, val = [1|0] , dir = out, mode = GPIO */
  211. void cfspi_cs_activate(uint bus, uint cs, uint cs_active_high)
  212. {
  213. debug("%s: bus %d cs %d cs_active_high %d\n",
  214. __func__, bus, cs, cs_active_high);
  215. switch (cs) {
  216. case 0: /* QSPI_CS[0] = PQSPI[3] */
  217. if (cs_active_high)
  218. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
  219. else
  220. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
  221. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  222. mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x08);
  223. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  224. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
  225. break;
  226. case 1: /* QSPI_CS[1] = PQSPI[4] */
  227. if (cs_active_high)
  228. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
  229. else
  230. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
  231. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  232. mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x10);
  233. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  234. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
  235. break;
  236. case 2: /* QSPI_CS[2] = PTIMER[7] */
  237. if (cs_active_high)
  238. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
  239. else
  240. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
  241. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  242. mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x80);
  243. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  244. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
  245. break;
  246. case 3: /* QSPI_CS[3] = PTIMER[3] */
  247. if (cs_active_high)
  248. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
  249. else
  250. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
  251. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  252. mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x08);
  253. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  254. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
  255. break;
  256. }
  257. }
  258. /* Deassert chip select, val = [1|0], dir = in, mode = GPIO
  259. * direction set as IN to undrive the pin, external pullup/pulldown will bring
  260. * bus to deassert state.
  261. */
  262. void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high)
  263. {
  264. debug("%s: bus %d cs %d cs_active_high %d\n",
  265. __func__, bus, cs, cs_active_high);
  266. switch (cs) {
  267. case 0: /* QSPI_CS[0] = PQSPI[3] */
  268. if (cs_active_high)
  269. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
  270. else
  271. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
  272. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  273. mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xF7);
  274. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  275. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
  276. break;
  277. case 1: /* QSPI_CS[1] = PQSPI[4] */
  278. if (cs_active_high)
  279. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
  280. else
  281. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
  282. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  283. mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xEF);
  284. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  285. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
  286. break;
  287. case 2: /* QSPI_CS[2] = PTIMER[7] */
  288. if (cs_active_high)
  289. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
  290. else
  291. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
  292. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  293. mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0x7F);
  294. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  295. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
  296. break;
  297. case 3: /* QSPI_CS[3] = PTIMER[3] */
  298. if (cs_active_high)
  299. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
  300. else
  301. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
  302. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  303. mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0xF7);
  304. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  305. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
  306. break;
  307. }
  308. }
  309. #endif /* CONFIG_M5271 */