cf_qspi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 = malloc(sizeof(struct cf_qspi_slave));
  106. if (!dev)
  107. return NULL;
  108. /* Initialize to known value */
  109. dev->slave.bus = bus;
  110. dev->slave.cs = cs;
  111. dev->regs = (qspi_t *)MMAP_QSPI;
  112. dev->qmr = 0;
  113. dev->qwr = 0;
  114. dev->qcr = 0;
  115. /* Map max_hz to QMR[BAUD] */
  116. if (max_hz == 0) /* Go as fast as possible */
  117. dev->qmr = 2u;
  118. else /* Get the closest baud rate */
  119. dev->qmr = clamp(((gd->bus_clk >> 2) + max_hz - 1)/max_hz,
  120. 2u, 255u);
  121. /* Map mode to QMR[CPOL] and QMR[CPHA] */
  122. if (mode & SPI_CPOL)
  123. dev->qmr |= QSPI_QMR_CPOL;
  124. if (mode & SPI_CPHA)
  125. dev->qmr |= QSPI_QMR_CPHA;
  126. /* Hardcode bit length to 8 bit per transter */
  127. dev->qmr |= QSPI_QMR_BITS_8;
  128. /* Set QMR[MSTR] to enable QSPI as master */
  129. dev->qmr |= QSPI_QMR_MSTR;
  130. /*
  131. * Set QCR and QWR to default values for spi flash operation.
  132. * If more custom QCR and QRW are needed, overload mode variable
  133. */
  134. dev->qcr = (QSPI_QDR_CONT | QSPI_QDR_BITSE);
  135. if (!(mode & SPI_CS_HIGH))
  136. dev->qwr |= QSPI_QWR_CSIV;
  137. return &dev->slave;
  138. }
  139. /* Transfer 8 bit at a time */
  140. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  141. void *din, unsigned long flags)
  142. {
  143. struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
  144. volatile qspi_t *qspi = dev->regs;
  145. u8 *txbuf = (u8 *)dout;
  146. u8 *rxbuf = (u8 *)din;
  147. u32 count = ((bitlen / 8) + (bitlen % 8 ? 1 : 0));
  148. u32 n, i = 0;
  149. /* Sanitize arguments */
  150. if (slave == NULL) {
  151. printf("%s: NULL slave ptr\n", __func__);
  152. return -1;
  153. }
  154. if (flags & SPI_XFER_BEGIN)
  155. spi_cs_activate(slave);
  156. /* There is something to send, lets process it. spi_xfer is also called
  157. * just to toggle chip select, so bitlen of 0 is valid */
  158. if (count > 0) {
  159. /*
  160. * NOTE: Since chip select is driven as a bit-bang-ed GPIO
  161. * using spi_cs_activate() and spi_cs_deactivate(),
  162. * the chip select settings inside the controller
  163. * (i.e. QCR[CONT] and QWR[CSIV]) are moot. The bits are set to
  164. * keep the controller settings consistent with the actual
  165. * operation of the bus.
  166. */
  167. /* Write the slave device's settings for the controller.*/
  168. write_qmr(qspi, dev->qmr);
  169. write_qwr(qspi, dev->qwr);
  170. /* Limit transfer to 16 at a time */
  171. n = min(count, 16u);
  172. do {
  173. /* Setup queue end point */
  174. write_qwr(qspi, ((read_qwr(qspi) & QSPI_QWR_ENDQP_MASK)
  175. | QSPI_QWR_ENDQP((n-1))));
  176. /* Write Command RAM */
  177. write_qar(qspi, QSPI_QAR_CMD);
  178. for (i = 0; i < n; ++i)
  179. write_qdr(qspi, dev->qcr);
  180. /* Write TxBuf, if none given, fill with ZEROes */
  181. write_qar(qspi, QSPI_QAR_TRANS);
  182. if (txbuf) {
  183. for (i = 0; i < n; ++i)
  184. write_qdr(qspi, *txbuf++);
  185. } else {
  186. for (i = 0; i < n; ++i)
  187. write_qdr(qspi, 0);
  188. }
  189. /* Clear QIR[SPIF] by writing a 1 to it */
  190. write_qir(qspi, read_qir(qspi) | QSPI_QIR_SPIF);
  191. /* Set QDLYR[SPE] to start sending */
  192. write_qdlyr(qspi, read_qdlyr(qspi) | QSPI_QDLYR_SPE);
  193. /* Poll QIR[SPIF] for transfer completion */
  194. while ((read_qir(qspi) & QSPI_QIR_SPIF) != 1)
  195. udelay(1);
  196. /* If given read RxBuf, load data to it */
  197. if (rxbuf) {
  198. write_qar(qspi, QSPI_QAR_RECV);
  199. for (i = 0; i < n; ++i)
  200. *rxbuf++ = read_qdr(qspi);
  201. }
  202. /* Decrement count */
  203. count -= n;
  204. } while (count);
  205. }
  206. if (flags & SPI_XFER_END)
  207. spi_cs_deactivate(slave);
  208. return 0;
  209. }
  210. /* Each MCF CPU may have different pin assignments for chip selects. */
  211. #if defined(CONFIG_M5271)
  212. /* Assert chip select, val = [1|0] , dir = out, mode = GPIO */
  213. void cfspi_cs_activate(uint bus, uint cs, uint cs_active_high)
  214. {
  215. debug("%s: bus %d cs %d cs_active_high %d\n",
  216. __func__, bus, cs, cs_active_high);
  217. switch (cs) {
  218. case 0: /* QSPI_CS[0] = PQSPI[3] */
  219. if (cs_active_high)
  220. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
  221. else
  222. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
  223. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  224. mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x08);
  225. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  226. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
  227. break;
  228. case 1: /* QSPI_CS[1] = PQSPI[4] */
  229. if (cs_active_high)
  230. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
  231. else
  232. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
  233. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  234. mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x10);
  235. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  236. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
  237. break;
  238. case 2: /* QSPI_CS[2] = PTIMER[7] */
  239. if (cs_active_high)
  240. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
  241. else
  242. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
  243. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  244. mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x80);
  245. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  246. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
  247. break;
  248. case 3: /* QSPI_CS[3] = PTIMER[3] */
  249. if (cs_active_high)
  250. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
  251. else
  252. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
  253. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  254. mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x08);
  255. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  256. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
  257. break;
  258. }
  259. }
  260. /* Deassert chip select, val = [1|0], dir = in, mode = GPIO
  261. * direction set as IN to undrive the pin, external pullup/pulldown will bring
  262. * bus to deassert state.
  263. */
  264. void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high)
  265. {
  266. debug("%s: bus %d cs %d cs_active_high %d\n",
  267. __func__, bus, cs, cs_active_high);
  268. switch (cs) {
  269. case 0: /* QSPI_CS[0] = PQSPI[3] */
  270. if (cs_active_high)
  271. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
  272. else
  273. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
  274. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  275. mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xF7);
  276. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  277. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
  278. break;
  279. case 1: /* QSPI_CS[1] = PQSPI[4] */
  280. if (cs_active_high)
  281. mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
  282. else
  283. mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
  284. mbar_writeByte(MCF_GPIO_PDDR_QSPI,
  285. mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xEF);
  286. mbar_writeByte(MCF_GPIO_PAR_QSPI,
  287. mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
  288. break;
  289. case 2: /* QSPI_CS[2] = PTIMER[7] */
  290. if (cs_active_high)
  291. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
  292. else
  293. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
  294. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  295. mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0x7F);
  296. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  297. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
  298. break;
  299. case 3: /* QSPI_CS[3] = PTIMER[3] */
  300. if (cs_active_high)
  301. mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
  302. else
  303. mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
  304. mbar_writeByte(MCF_GPIO_PDDR_TIMER,
  305. mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0xF7);
  306. mbar_writeShort(MCF_GPIO_PAR_TIMER,
  307. mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
  308. break;
  309. }
  310. }
  311. #endif /* CONFIG_M5271 */