spi-bcm63xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Broadcom BCM63xx SPI controller support
  3. *
  4. * Copyright (C) 2009-2012 Florian Fainelli <florian@openwrt.org>
  5. * Copyright (C) 2010 Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/delay.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/completion.h>
  30. #include <linux/err.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/pm_runtime.h>
  33. #include <bcm63xx_dev_spi.h>
  34. #define BCM63XX_SPI_MAX_PREPEND 15
  35. struct bcm63xx_spi {
  36. struct completion done;
  37. void __iomem *regs;
  38. int irq;
  39. /* Platform data */
  40. unsigned fifo_size;
  41. unsigned int msg_type_shift;
  42. unsigned int msg_ctl_width;
  43. /* data iomem */
  44. u8 __iomem *tx_io;
  45. const u8 __iomem *rx_io;
  46. struct clk *clk;
  47. struct platform_device *pdev;
  48. };
  49. static inline u8 bcm_spi_readb(struct bcm63xx_spi *bs,
  50. unsigned int offset)
  51. {
  52. return bcm_readb(bs->regs + bcm63xx_spireg(offset));
  53. }
  54. static inline u16 bcm_spi_readw(struct bcm63xx_spi *bs,
  55. unsigned int offset)
  56. {
  57. return bcm_readw(bs->regs + bcm63xx_spireg(offset));
  58. }
  59. static inline void bcm_spi_writeb(struct bcm63xx_spi *bs,
  60. u8 value, unsigned int offset)
  61. {
  62. bcm_writeb(value, bs->regs + bcm63xx_spireg(offset));
  63. }
  64. static inline void bcm_spi_writew(struct bcm63xx_spi *bs,
  65. u16 value, unsigned int offset)
  66. {
  67. bcm_writew(value, bs->regs + bcm63xx_spireg(offset));
  68. }
  69. static const unsigned bcm63xx_spi_freq_table[SPI_CLK_MASK][2] = {
  70. { 20000000, SPI_CLK_20MHZ },
  71. { 12500000, SPI_CLK_12_50MHZ },
  72. { 6250000, SPI_CLK_6_250MHZ },
  73. { 3125000, SPI_CLK_3_125MHZ },
  74. { 1563000, SPI_CLK_1_563MHZ },
  75. { 781000, SPI_CLK_0_781MHZ },
  76. { 391000, SPI_CLK_0_391MHZ }
  77. };
  78. static void bcm63xx_spi_setup_transfer(struct spi_device *spi,
  79. struct spi_transfer *t)
  80. {
  81. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  82. u8 clk_cfg, reg;
  83. int i;
  84. /* Find the closest clock configuration */
  85. for (i = 0; i < SPI_CLK_MASK; i++) {
  86. if (t->speed_hz >= bcm63xx_spi_freq_table[i][0]) {
  87. clk_cfg = bcm63xx_spi_freq_table[i][1];
  88. break;
  89. }
  90. }
  91. /* No matching configuration found, default to lowest */
  92. if (i == SPI_CLK_MASK)
  93. clk_cfg = SPI_CLK_0_391MHZ;
  94. /* clear existing clock configuration bits of the register */
  95. reg = bcm_spi_readb(bs, SPI_CLK_CFG);
  96. reg &= ~SPI_CLK_MASK;
  97. reg |= clk_cfg;
  98. bcm_spi_writeb(bs, reg, SPI_CLK_CFG);
  99. dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n",
  100. clk_cfg, t->speed_hz);
  101. }
  102. /* the spi->mode bits understood by this driver: */
  103. #define MODEBITS (SPI_CPOL | SPI_CPHA)
  104. static int bcm63xx_txrx_bufs(struct spi_device *spi, struct spi_transfer *first,
  105. unsigned int num_transfers)
  106. {
  107. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  108. u16 msg_ctl;
  109. u16 cmd;
  110. u8 rx_tail;
  111. unsigned int i, timeout = 0, prepend_len = 0, len = 0;
  112. struct spi_transfer *t = first;
  113. bool do_rx = false;
  114. bool do_tx = false;
  115. /* Disable the CMD_DONE interrupt */
  116. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  117. dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
  118. t->tx_buf, t->rx_buf, t->len);
  119. if (num_transfers > 1 && t->tx_buf && t->len <= BCM63XX_SPI_MAX_PREPEND)
  120. prepend_len = t->len;
  121. /* prepare the buffer */
  122. for (i = 0; i < num_transfers; i++) {
  123. if (t->tx_buf) {
  124. do_tx = true;
  125. memcpy_toio(bs->tx_io + len, t->tx_buf, t->len);
  126. /* don't prepend more than one tx */
  127. if (t != first)
  128. prepend_len = 0;
  129. }
  130. if (t->rx_buf) {
  131. do_rx = true;
  132. /* prepend is half-duplex write only */
  133. if (t == first)
  134. prepend_len = 0;
  135. }
  136. len += t->len;
  137. t = list_entry(t->transfer_list.next, struct spi_transfer,
  138. transfer_list);
  139. }
  140. reinit_completion(&bs->done);
  141. /* Fill in the Message control register */
  142. msg_ctl = (len << SPI_BYTE_CNT_SHIFT);
  143. if (do_rx && do_tx && prepend_len == 0)
  144. msg_ctl |= (SPI_FD_RW << bs->msg_type_shift);
  145. else if (do_rx)
  146. msg_ctl |= (SPI_HD_R << bs->msg_type_shift);
  147. else if (do_tx)
  148. msg_ctl |= (SPI_HD_W << bs->msg_type_shift);
  149. switch (bs->msg_ctl_width) {
  150. case 8:
  151. bcm_spi_writeb(bs, msg_ctl, SPI_MSG_CTL);
  152. break;
  153. case 16:
  154. bcm_spi_writew(bs, msg_ctl, SPI_MSG_CTL);
  155. break;
  156. }
  157. /* Issue the transfer */
  158. cmd = SPI_CMD_START_IMMEDIATE;
  159. cmd |= (prepend_len << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
  160. cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT);
  161. bcm_spi_writew(bs, cmd, SPI_CMD);
  162. /* Enable the CMD_DONE interrupt */
  163. bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
  164. timeout = wait_for_completion_timeout(&bs->done, HZ);
  165. if (!timeout)
  166. return -ETIMEDOUT;
  167. if (!do_rx)
  168. return 0;
  169. len = 0;
  170. t = first;
  171. /* Read out all the data */
  172. for (i = 0; i < num_transfers; i++) {
  173. if (t->rx_buf)
  174. memcpy_fromio(t->rx_buf, bs->rx_io + len, t->len);
  175. if (t != first || prepend_len == 0)
  176. len += t->len;
  177. t = list_entry(t->transfer_list.next, struct spi_transfer,
  178. transfer_list);
  179. }
  180. return 0;
  181. }
  182. static int bcm63xx_spi_transfer_one(struct spi_master *master,
  183. struct spi_message *m)
  184. {
  185. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  186. struct spi_transfer *t, *first = NULL;
  187. struct spi_device *spi = m->spi;
  188. int status = 0;
  189. unsigned int n_transfers = 0, total_len = 0;
  190. bool can_use_prepend = false;
  191. /*
  192. * This SPI controller does not support keeping CS active after a
  193. * transfer.
  194. * Work around this by merging as many transfers we can into one big
  195. * full-duplex transfers.
  196. */
  197. list_for_each_entry(t, &m->transfers, transfer_list) {
  198. if (!first)
  199. first = t;
  200. n_transfers++;
  201. total_len += t->len;
  202. if (n_transfers == 2 && !first->rx_buf && !t->tx_buf &&
  203. first->len <= BCM63XX_SPI_MAX_PREPEND)
  204. can_use_prepend = true;
  205. else if (can_use_prepend && t->tx_buf)
  206. can_use_prepend = false;
  207. /* we can only transfer one fifo worth of data */
  208. if ((can_use_prepend &&
  209. total_len > (bs->fifo_size + BCM63XX_SPI_MAX_PREPEND)) ||
  210. (!can_use_prepend && total_len > bs->fifo_size)) {
  211. dev_err(&spi->dev, "unable to do transfers larger than FIFO size (%i > %i)\n",
  212. total_len, bs->fifo_size);
  213. status = -EINVAL;
  214. goto exit;
  215. }
  216. /* all combined transfers have to have the same speed */
  217. if (t->speed_hz != first->speed_hz) {
  218. dev_err(&spi->dev, "unable to change speed between transfers\n");
  219. status = -EINVAL;
  220. goto exit;
  221. }
  222. /* CS will be deasserted directly after transfer */
  223. if (t->delay_usecs) {
  224. dev_err(&spi->dev, "unable to keep CS asserted after transfer\n");
  225. status = -EINVAL;
  226. goto exit;
  227. }
  228. if (t->cs_change ||
  229. list_is_last(&t->transfer_list, &m->transfers)) {
  230. /* configure adapter for a new transfer */
  231. bcm63xx_spi_setup_transfer(spi, first);
  232. /* send the data */
  233. status = bcm63xx_txrx_bufs(spi, first, n_transfers);
  234. if (status)
  235. goto exit;
  236. m->actual_length += total_len;
  237. first = NULL;
  238. n_transfers = 0;
  239. total_len = 0;
  240. can_use_prepend = false;
  241. }
  242. }
  243. exit:
  244. m->status = status;
  245. spi_finalize_current_message(master);
  246. return 0;
  247. }
  248. /* This driver supports single master mode only. Hence
  249. * CMD_DONE is the only interrupt we care about
  250. */
  251. static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id)
  252. {
  253. struct spi_master *master = (struct spi_master *)dev_id;
  254. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  255. u8 intr;
  256. /* Read interupts and clear them immediately */
  257. intr = bcm_spi_readb(bs, SPI_INT_STATUS);
  258. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  259. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  260. /* A transfer completed */
  261. if (intr & SPI_INTR_CMD_DONE)
  262. complete(&bs->done);
  263. return IRQ_HANDLED;
  264. }
  265. static int bcm63xx_spi_probe(struct platform_device *pdev)
  266. {
  267. struct resource *r;
  268. struct device *dev = &pdev->dev;
  269. struct bcm63xx_spi_pdata *pdata = dev_get_platdata(&pdev->dev);
  270. int irq;
  271. struct spi_master *master;
  272. struct clk *clk;
  273. struct bcm63xx_spi *bs;
  274. int ret;
  275. irq = platform_get_irq(pdev, 0);
  276. if (irq < 0) {
  277. dev_err(dev, "no irq\n");
  278. return -ENXIO;
  279. }
  280. clk = devm_clk_get(dev, "spi");
  281. if (IS_ERR(clk)) {
  282. dev_err(dev, "no clock for device\n");
  283. return PTR_ERR(clk);
  284. }
  285. master = spi_alloc_master(dev, sizeof(*bs));
  286. if (!master) {
  287. dev_err(dev, "out of memory\n");
  288. return -ENOMEM;
  289. }
  290. bs = spi_master_get_devdata(master);
  291. init_completion(&bs->done);
  292. platform_set_drvdata(pdev, master);
  293. bs->pdev = pdev;
  294. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. bs->regs = devm_ioremap_resource(&pdev->dev, r);
  296. if (IS_ERR(bs->regs)) {
  297. ret = PTR_ERR(bs->regs);
  298. goto out_err;
  299. }
  300. bs->irq = irq;
  301. bs->clk = clk;
  302. bs->fifo_size = pdata->fifo_size;
  303. ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0,
  304. pdev->name, master);
  305. if (ret) {
  306. dev_err(dev, "unable to request irq\n");
  307. goto out_err;
  308. }
  309. master->bus_num = pdata->bus_num;
  310. master->num_chipselect = pdata->num_chipselect;
  311. master->transfer_one_message = bcm63xx_spi_transfer_one;
  312. master->mode_bits = MODEBITS;
  313. master->bits_per_word_mask = SPI_BPW_MASK(8);
  314. master->auto_runtime_pm = true;
  315. bs->msg_type_shift = pdata->msg_type_shift;
  316. bs->msg_ctl_width = pdata->msg_ctl_width;
  317. bs->tx_io = (u8 *)(bs->regs + bcm63xx_spireg(SPI_MSG_DATA));
  318. bs->rx_io = (const u8 *)(bs->regs + bcm63xx_spireg(SPI_RX_DATA));
  319. switch (bs->msg_ctl_width) {
  320. case 8:
  321. case 16:
  322. break;
  323. default:
  324. dev_err(dev, "unsupported MSG_CTL width: %d\n",
  325. bs->msg_ctl_width);
  326. goto out_err;
  327. }
  328. /* Initialize hardware */
  329. ret = clk_prepare_enable(bs->clk);
  330. if (ret)
  331. goto out_err;
  332. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  333. /* register and we are done */
  334. ret = devm_spi_register_master(dev, master);
  335. if (ret) {
  336. dev_err(dev, "spi register failed\n");
  337. goto out_clk_disable;
  338. }
  339. dev_info(dev, "at 0x%08x (irq %d, FIFOs size %d)\n",
  340. r->start, irq, bs->fifo_size);
  341. return 0;
  342. out_clk_disable:
  343. clk_disable_unprepare(clk);
  344. out_err:
  345. spi_master_put(master);
  346. return ret;
  347. }
  348. static int bcm63xx_spi_remove(struct platform_device *pdev)
  349. {
  350. struct spi_master *master = platform_get_drvdata(pdev);
  351. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  352. /* reset spi block */
  353. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  354. /* HW shutdown */
  355. clk_disable_unprepare(bs->clk);
  356. return 0;
  357. }
  358. #ifdef CONFIG_PM_SLEEP
  359. static int bcm63xx_spi_suspend(struct device *dev)
  360. {
  361. struct spi_master *master = dev_get_drvdata(dev);
  362. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  363. spi_master_suspend(master);
  364. clk_disable_unprepare(bs->clk);
  365. return 0;
  366. }
  367. static int bcm63xx_spi_resume(struct device *dev)
  368. {
  369. struct spi_master *master = dev_get_drvdata(dev);
  370. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  371. int ret;
  372. ret = clk_prepare_enable(bs->clk);
  373. if (ret)
  374. return ret;
  375. spi_master_resume(master);
  376. return 0;
  377. }
  378. #endif
  379. static const struct dev_pm_ops bcm63xx_spi_pm_ops = {
  380. SET_SYSTEM_SLEEP_PM_OPS(bcm63xx_spi_suspend, bcm63xx_spi_resume)
  381. };
  382. static struct platform_driver bcm63xx_spi_driver = {
  383. .driver = {
  384. .name = "bcm63xx-spi",
  385. .owner = THIS_MODULE,
  386. .pm = &bcm63xx_spi_pm_ops,
  387. },
  388. .probe = bcm63xx_spi_probe,
  389. .remove = bcm63xx_spi_remove,
  390. };
  391. module_platform_driver(bcm63xx_spi_driver);
  392. MODULE_ALIAS("platform:bcm63xx_spi");
  393. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  394. MODULE_AUTHOR("Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>");
  395. MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
  396. MODULE_LICENSE("GPL");