spi-falcon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2012 Thomas Langer <thomas.langer@lantiq.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/delay.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <lantiq_soc.h>
  16. #define DRV_NAME "sflash-falcon"
  17. #define FALCON_SPI_XFER_BEGIN (1 << 0)
  18. #define FALCON_SPI_XFER_END (1 << 1)
  19. /* Bus Read Configuration Register0 */
  20. #define BUSRCON0 0x00000010
  21. /* Bus Write Configuration Register0 */
  22. #define BUSWCON0 0x00000018
  23. /* Serial Flash Configuration Register */
  24. #define SFCON 0x00000080
  25. /* Serial Flash Time Register */
  26. #define SFTIME 0x00000084
  27. /* Serial Flash Status Register */
  28. #define SFSTAT 0x00000088
  29. /* Serial Flash Command Register */
  30. #define SFCMD 0x0000008C
  31. /* Serial Flash Address Register */
  32. #define SFADDR 0x00000090
  33. /* Serial Flash Data Register */
  34. #define SFDATA 0x00000094
  35. /* Serial Flash I/O Control Register */
  36. #define SFIO 0x00000098
  37. /* EBU Clock Control Register */
  38. #define EBUCC 0x000000C4
  39. /* Dummy Phase Length */
  40. #define SFCMD_DUMLEN_OFFSET 16
  41. #define SFCMD_DUMLEN_MASK 0x000F0000
  42. /* Chip Select */
  43. #define SFCMD_CS_OFFSET 24
  44. #define SFCMD_CS_MASK 0x07000000
  45. /* field offset */
  46. #define SFCMD_ALEN_OFFSET 20
  47. #define SFCMD_ALEN_MASK 0x00700000
  48. /* SCK Rise-edge Position */
  49. #define SFTIME_SCKR_POS_OFFSET 8
  50. #define SFTIME_SCKR_POS_MASK 0x00000F00
  51. /* SCK Period */
  52. #define SFTIME_SCK_PER_OFFSET 0
  53. #define SFTIME_SCK_PER_MASK 0x0000000F
  54. /* SCK Fall-edge Position */
  55. #define SFTIME_SCKF_POS_OFFSET 12
  56. #define SFTIME_SCKF_POS_MASK 0x0000F000
  57. /* Device Size */
  58. #define SFCON_DEV_SIZE_A23_0 0x03000000
  59. #define SFCON_DEV_SIZE_MASK 0x0F000000
  60. /* Read Data Position */
  61. #define SFTIME_RD_POS_MASK 0x000F0000
  62. /* Data Output */
  63. #define SFIO_UNUSED_WD_MASK 0x0000000F
  64. /* Command Opcode mask */
  65. #define SFCMD_OPC_MASK 0x000000FF
  66. /* dlen bytes of data to write */
  67. #define SFCMD_DIR_WRITE 0x00000100
  68. /* Data Length offset */
  69. #define SFCMD_DLEN_OFFSET 9
  70. /* Command Error */
  71. #define SFSTAT_CMD_ERR 0x20000000
  72. /* Access Command Pending */
  73. #define SFSTAT_CMD_PEND 0x00400000
  74. /* Frequency set to 100MHz. */
  75. #define EBUCC_EBUDIV_SELF100 0x00000001
  76. /* Serial Flash */
  77. #define BUSRCON0_AGEN_SERIAL_FLASH 0xF0000000
  78. /* 8-bit multiplexed */
  79. #define BUSRCON0_PORTW_8_BIT_MUX 0x00000000
  80. /* Serial Flash */
  81. #define BUSWCON0_AGEN_SERIAL_FLASH 0xF0000000
  82. /* Chip Select after opcode */
  83. #define SFCMD_KEEP_CS_KEEP_SELECTED 0x00008000
  84. #define CLOCK_100M 100000000
  85. #define CLOCK_50M 50000000
  86. struct falcon_sflash {
  87. u32 sfcmd; /* for caching of opcode, direction, ... */
  88. struct spi_master *master;
  89. };
  90. int falcon_sflash_xfer(struct spi_device *spi, struct spi_transfer *t,
  91. unsigned long flags)
  92. {
  93. struct device *dev = &spi->dev;
  94. struct falcon_sflash *priv = spi_master_get_devdata(spi->master);
  95. const u8 *txp = t->tx_buf;
  96. u8 *rxp = t->rx_buf;
  97. unsigned int bytelen = ((8 * t->len + 7) / 8);
  98. unsigned int len, alen, dumlen;
  99. u32 val;
  100. enum {
  101. state_init,
  102. state_command_prepare,
  103. state_write,
  104. state_read,
  105. state_disable_cs,
  106. state_end
  107. } state = state_init;
  108. do {
  109. switch (state) {
  110. case state_init: /* detect phase of upper layer sequence */
  111. {
  112. /* initial write ? */
  113. if (flags & FALCON_SPI_XFER_BEGIN) {
  114. if (!txp) {
  115. dev_err(dev,
  116. "BEGIN without tx data!\n");
  117. return -ENODATA;
  118. }
  119. /*
  120. * Prepare the parts of the sfcmd register,
  121. * which should not change during a sequence!
  122. * Only exception are the length fields,
  123. * especially alen and dumlen.
  124. */
  125. priv->sfcmd = ((spi->chip_select
  126. << SFCMD_CS_OFFSET)
  127. & SFCMD_CS_MASK);
  128. priv->sfcmd |= SFCMD_KEEP_CS_KEEP_SELECTED;
  129. priv->sfcmd |= *txp;
  130. txp++;
  131. bytelen--;
  132. if (bytelen) {
  133. /*
  134. * more data:
  135. * maybe address and/or dummy
  136. */
  137. state = state_command_prepare;
  138. break;
  139. } else {
  140. dev_dbg(dev, "write cmd %02X\n",
  141. priv->sfcmd & SFCMD_OPC_MASK);
  142. }
  143. }
  144. /* continued write ? */
  145. if (txp && bytelen) {
  146. state = state_write;
  147. break;
  148. }
  149. /* read data? */
  150. if (rxp && bytelen) {
  151. state = state_read;
  152. break;
  153. }
  154. /* end of sequence? */
  155. if (flags & FALCON_SPI_XFER_END)
  156. state = state_disable_cs;
  157. else
  158. state = state_end;
  159. break;
  160. }
  161. /* collect tx data for address and dummy phase */
  162. case state_command_prepare:
  163. {
  164. /* txp is valid, already checked */
  165. val = 0;
  166. alen = 0;
  167. dumlen = 0;
  168. while (bytelen > 0) {
  169. if (alen < 3) {
  170. val = (val << 8) | (*txp++);
  171. alen++;
  172. } else if ((dumlen < 15) && (*txp == 0)) {
  173. /*
  174. * assume dummy bytes are set to 0
  175. * from upper layer
  176. */
  177. dumlen++;
  178. txp++;
  179. } else {
  180. break;
  181. }
  182. bytelen--;
  183. }
  184. priv->sfcmd &= ~(SFCMD_ALEN_MASK | SFCMD_DUMLEN_MASK);
  185. priv->sfcmd |= (alen << SFCMD_ALEN_OFFSET) |
  186. (dumlen << SFCMD_DUMLEN_OFFSET);
  187. if (alen > 0)
  188. ltq_ebu_w32(val, SFADDR);
  189. dev_dbg(dev, "wr %02X, alen=%d (addr=%06X) dlen=%d\n",
  190. priv->sfcmd & SFCMD_OPC_MASK,
  191. alen, val, dumlen);
  192. if (bytelen > 0) {
  193. /* continue with write */
  194. state = state_write;
  195. } else if (flags & FALCON_SPI_XFER_END) {
  196. /* end of sequence? */
  197. state = state_disable_cs;
  198. } else {
  199. /*
  200. * go to end and expect another
  201. * call (read or write)
  202. */
  203. state = state_end;
  204. }
  205. break;
  206. }
  207. case state_write:
  208. {
  209. /* txp still valid */
  210. priv->sfcmd |= SFCMD_DIR_WRITE;
  211. len = 0;
  212. val = 0;
  213. do {
  214. if (bytelen--)
  215. val |= (*txp++) << (8 * len++);
  216. if ((flags & FALCON_SPI_XFER_END)
  217. && (bytelen == 0)) {
  218. priv->sfcmd &=
  219. ~SFCMD_KEEP_CS_KEEP_SELECTED;
  220. }
  221. if ((len == 4) || (bytelen == 0)) {
  222. ltq_ebu_w32(val, SFDATA);
  223. ltq_ebu_w32(priv->sfcmd
  224. | (len<<SFCMD_DLEN_OFFSET),
  225. SFCMD);
  226. len = 0;
  227. val = 0;
  228. priv->sfcmd &= ~(SFCMD_ALEN_MASK
  229. | SFCMD_DUMLEN_MASK);
  230. }
  231. } while (bytelen);
  232. state = state_end;
  233. break;
  234. }
  235. case state_read:
  236. {
  237. /* read data */
  238. priv->sfcmd &= ~SFCMD_DIR_WRITE;
  239. do {
  240. if ((flags & FALCON_SPI_XFER_END)
  241. && (bytelen <= 4)) {
  242. priv->sfcmd &=
  243. ~SFCMD_KEEP_CS_KEEP_SELECTED;
  244. }
  245. len = (bytelen > 4) ? 4 : bytelen;
  246. bytelen -= len;
  247. ltq_ebu_w32(priv->sfcmd
  248. | (len << SFCMD_DLEN_OFFSET), SFCMD);
  249. priv->sfcmd &= ~(SFCMD_ALEN_MASK
  250. | SFCMD_DUMLEN_MASK);
  251. do {
  252. val = ltq_ebu_r32(SFSTAT);
  253. if (val & SFSTAT_CMD_ERR) {
  254. /* reset error status */
  255. dev_err(dev, "SFSTAT: CMD_ERR");
  256. dev_err(dev, " (%x)\n", val);
  257. ltq_ebu_w32(SFSTAT_CMD_ERR,
  258. SFSTAT);
  259. return -EBADE;
  260. }
  261. } while (val & SFSTAT_CMD_PEND);
  262. val = ltq_ebu_r32(SFDATA);
  263. do {
  264. *rxp = (val & 0xFF);
  265. rxp++;
  266. val >>= 8;
  267. len--;
  268. } while (len);
  269. } while (bytelen);
  270. state = state_end;
  271. break;
  272. }
  273. case state_disable_cs:
  274. {
  275. priv->sfcmd &= ~SFCMD_KEEP_CS_KEEP_SELECTED;
  276. ltq_ebu_w32(priv->sfcmd | (0 << SFCMD_DLEN_OFFSET),
  277. SFCMD);
  278. val = ltq_ebu_r32(SFSTAT);
  279. if (val & SFSTAT_CMD_ERR) {
  280. /* reset error status */
  281. dev_err(dev, "SFSTAT: CMD_ERR (%x)\n", val);
  282. ltq_ebu_w32(SFSTAT_CMD_ERR, SFSTAT);
  283. return -EBADE;
  284. }
  285. state = state_end;
  286. break;
  287. }
  288. case state_end:
  289. break;
  290. }
  291. } while (state != state_end);
  292. return 0;
  293. }
  294. static int falcon_sflash_setup(struct spi_device *spi)
  295. {
  296. unsigned int i;
  297. unsigned long flags;
  298. spin_lock_irqsave(&ebu_lock, flags);
  299. if (spi->max_speed_hz >= CLOCK_100M) {
  300. /* set EBU clock to 100 MHz */
  301. ltq_sys1_w32_mask(0, EBUCC_EBUDIV_SELF100, EBUCC);
  302. i = 1; /* divider */
  303. } else {
  304. /* set EBU clock to 50 MHz */
  305. ltq_sys1_w32_mask(EBUCC_EBUDIV_SELF100, 0, EBUCC);
  306. /* search for suitable divider */
  307. for (i = 1; i < 7; i++) {
  308. if (CLOCK_50M / i <= spi->max_speed_hz)
  309. break;
  310. }
  311. }
  312. /* setup period of serial clock */
  313. ltq_ebu_w32_mask(SFTIME_SCKF_POS_MASK
  314. | SFTIME_SCKR_POS_MASK
  315. | SFTIME_SCK_PER_MASK,
  316. (i << SFTIME_SCKR_POS_OFFSET)
  317. | (i << (SFTIME_SCK_PER_OFFSET + 1)),
  318. SFTIME);
  319. /*
  320. * set some bits of unused_wd, to not trigger HOLD/WP
  321. * signals on non QUAD flashes
  322. */
  323. ltq_ebu_w32((SFIO_UNUSED_WD_MASK & (0x8 | 0x4)), SFIO);
  324. ltq_ebu_w32(BUSRCON0_AGEN_SERIAL_FLASH | BUSRCON0_PORTW_8_BIT_MUX,
  325. BUSRCON0);
  326. ltq_ebu_w32(BUSWCON0_AGEN_SERIAL_FLASH, BUSWCON0);
  327. /* set address wrap around to maximum for 24-bit addresses */
  328. ltq_ebu_w32_mask(SFCON_DEV_SIZE_MASK, SFCON_DEV_SIZE_A23_0, SFCON);
  329. spin_unlock_irqrestore(&ebu_lock, flags);
  330. return 0;
  331. }
  332. static int falcon_sflash_xfer_one(struct spi_master *master,
  333. struct spi_message *m)
  334. {
  335. struct falcon_sflash *priv = spi_master_get_devdata(master);
  336. struct spi_transfer *t;
  337. unsigned long spi_flags;
  338. unsigned long flags;
  339. int ret = 0;
  340. priv->sfcmd = 0;
  341. m->actual_length = 0;
  342. spi_flags = FALCON_SPI_XFER_BEGIN;
  343. list_for_each_entry(t, &m->transfers, transfer_list) {
  344. if (list_is_last(&t->transfer_list, &m->transfers))
  345. spi_flags |= FALCON_SPI_XFER_END;
  346. spin_lock_irqsave(&ebu_lock, flags);
  347. ret = falcon_sflash_xfer(m->spi, t, spi_flags);
  348. spin_unlock_irqrestore(&ebu_lock, flags);
  349. if (ret)
  350. break;
  351. m->actual_length += t->len;
  352. WARN_ON(t->delay_usecs || t->cs_change);
  353. spi_flags = 0;
  354. }
  355. m->status = ret;
  356. spi_finalize_current_message(master);
  357. return 0;
  358. }
  359. static int falcon_sflash_probe(struct platform_device *pdev)
  360. {
  361. struct falcon_sflash *priv;
  362. struct spi_master *master;
  363. int ret;
  364. master = spi_alloc_master(&pdev->dev, sizeof(*priv));
  365. if (!master)
  366. return -ENOMEM;
  367. priv = spi_master_get_devdata(master);
  368. priv->master = master;
  369. master->mode_bits = SPI_MODE_3;
  370. master->flags = SPI_MASTER_HALF_DUPLEX;
  371. master->setup = falcon_sflash_setup;
  372. master->transfer_one_message = falcon_sflash_xfer_one;
  373. master->dev.of_node = pdev->dev.of_node;
  374. ret = devm_spi_register_master(&pdev->dev, master);
  375. if (ret)
  376. spi_master_put(master);
  377. return ret;
  378. }
  379. static const struct of_device_id falcon_sflash_match[] = {
  380. { .compatible = "lantiq,sflash-falcon" },
  381. {},
  382. };
  383. MODULE_DEVICE_TABLE(of, falcon_sflash_match);
  384. static struct platform_driver falcon_sflash_driver = {
  385. .probe = falcon_sflash_probe,
  386. .driver = {
  387. .name = DRV_NAME,
  388. .of_match_table = falcon_sflash_match,
  389. }
  390. };
  391. module_platform_driver(falcon_sflash_driver);
  392. MODULE_LICENSE("GPL");
  393. MODULE_DESCRIPTION("Lantiq Falcon SPI/SFLASH controller driver");