spi-bcm63xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/init.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/completion.h>
  31. #include <linux/err.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/pm_runtime.h>
  34. #include <bcm63xx_dev_spi.h>
  35. #define PFX KBUILD_MODNAME
  36. #define DRV_VER "0.1.2"
  37. struct bcm63xx_spi {
  38. spinlock_t lock;
  39. int stopping;
  40. struct completion done;
  41. void __iomem *regs;
  42. int irq;
  43. /* Platform data */
  44. u32 speed_hz;
  45. unsigned fifo_size;
  46. /* Data buffers */
  47. const unsigned char *tx_ptr;
  48. unsigned char *rx_ptr;
  49. /* data iomem */
  50. u8 __iomem *tx_io;
  51. const u8 __iomem *rx_io;
  52. int remaining_bytes;
  53. struct clk *clk;
  54. struct platform_device *pdev;
  55. };
  56. static inline u8 bcm_spi_readb(struct bcm63xx_spi *bs,
  57. unsigned int offset)
  58. {
  59. return bcm_readb(bs->regs + bcm63xx_spireg(offset));
  60. }
  61. static inline u16 bcm_spi_readw(struct bcm63xx_spi *bs,
  62. unsigned int offset)
  63. {
  64. return bcm_readw(bs->regs + bcm63xx_spireg(offset));
  65. }
  66. static inline void bcm_spi_writeb(struct bcm63xx_spi *bs,
  67. u8 value, unsigned int offset)
  68. {
  69. bcm_writeb(value, bs->regs + bcm63xx_spireg(offset));
  70. }
  71. static inline void bcm_spi_writew(struct bcm63xx_spi *bs,
  72. u16 value, unsigned int offset)
  73. {
  74. bcm_writew(value, bs->regs + bcm63xx_spireg(offset));
  75. }
  76. static const unsigned bcm63xx_spi_freq_table[SPI_CLK_MASK][2] = {
  77. { 20000000, SPI_CLK_20MHZ },
  78. { 12500000, SPI_CLK_12_50MHZ },
  79. { 6250000, SPI_CLK_6_250MHZ },
  80. { 3125000, SPI_CLK_3_125MHZ },
  81. { 1563000, SPI_CLK_1_563MHZ },
  82. { 781000, SPI_CLK_0_781MHZ },
  83. { 391000, SPI_CLK_0_391MHZ }
  84. };
  85. static int bcm63xx_spi_check_transfer(struct spi_device *spi,
  86. struct spi_transfer *t)
  87. {
  88. u8 bits_per_word;
  89. bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
  90. if (bits_per_word != 8) {
  91. dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
  92. __func__, bits_per_word);
  93. return -EINVAL;
  94. }
  95. if (spi->chip_select > spi->master->num_chipselect) {
  96. dev_err(&spi->dev, "%s, unsupported slave %d\n",
  97. __func__, spi->chip_select);
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. static void bcm63xx_spi_setup_transfer(struct spi_device *spi,
  103. struct spi_transfer *t)
  104. {
  105. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  106. u32 hz;
  107. u8 clk_cfg, reg;
  108. int i;
  109. hz = (t) ? t->speed_hz : spi->max_speed_hz;
  110. /* Find the closest clock configuration */
  111. for (i = 0; i < SPI_CLK_MASK; i++) {
  112. if (hz <= bcm63xx_spi_freq_table[i][0]) {
  113. clk_cfg = bcm63xx_spi_freq_table[i][1];
  114. break;
  115. }
  116. }
  117. /* No matching configuration found, default to lowest */
  118. if (i == SPI_CLK_MASK)
  119. clk_cfg = SPI_CLK_0_391MHZ;
  120. /* clear existing clock configuration bits of the register */
  121. reg = bcm_spi_readb(bs, SPI_CLK_CFG);
  122. reg &= ~SPI_CLK_MASK;
  123. reg |= clk_cfg;
  124. bcm_spi_writeb(bs, reg, SPI_CLK_CFG);
  125. dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n",
  126. clk_cfg, hz);
  127. }
  128. /* the spi->mode bits understood by this driver: */
  129. #define MODEBITS (SPI_CPOL | SPI_CPHA)
  130. static int bcm63xx_spi_setup(struct spi_device *spi)
  131. {
  132. struct bcm63xx_spi *bs;
  133. int ret;
  134. bs = spi_master_get_devdata(spi->master);
  135. if (bs->stopping)
  136. return -ESHUTDOWN;
  137. if (!spi->bits_per_word)
  138. spi->bits_per_word = 8;
  139. if (spi->mode & ~MODEBITS) {
  140. dev_err(&spi->dev, "%s, unsupported mode bits %x\n",
  141. __func__, spi->mode & ~MODEBITS);
  142. return -EINVAL;
  143. }
  144. ret = bcm63xx_spi_check_transfer(spi, NULL);
  145. if (ret < 0) {
  146. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  147. spi->mode & ~MODEBITS);
  148. return ret;
  149. }
  150. dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec/bit\n",
  151. __func__, spi->mode & MODEBITS, spi->bits_per_word, 0);
  152. return 0;
  153. }
  154. /* Fill the TX FIFO with as many bytes as possible */
  155. static void bcm63xx_spi_fill_tx_fifo(struct bcm63xx_spi *bs)
  156. {
  157. u8 size;
  158. /* Fill the Tx FIFO with as many bytes as possible */
  159. size = bs->remaining_bytes < bs->fifo_size ? bs->remaining_bytes :
  160. bs->fifo_size;
  161. memcpy_toio(bs->tx_io, bs->tx_ptr, size);
  162. bs->remaining_bytes -= size;
  163. }
  164. static unsigned int bcm63xx_txrx_bufs(struct spi_device *spi,
  165. struct spi_transfer *t)
  166. {
  167. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  168. u16 msg_ctl;
  169. u16 cmd;
  170. /* Disable the CMD_DONE interrupt */
  171. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  172. dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
  173. t->tx_buf, t->rx_buf, t->len);
  174. /* Transmitter is inhibited */
  175. bs->tx_ptr = t->tx_buf;
  176. bs->rx_ptr = t->rx_buf;
  177. if (t->tx_buf) {
  178. bs->remaining_bytes = t->len;
  179. bcm63xx_spi_fill_tx_fifo(bs);
  180. }
  181. init_completion(&bs->done);
  182. /* Fill in the Message control register */
  183. msg_ctl = (t->len << SPI_BYTE_CNT_SHIFT);
  184. if (t->rx_buf && t->tx_buf)
  185. msg_ctl |= (SPI_FD_RW << SPI_MSG_TYPE_SHIFT);
  186. else if (t->rx_buf)
  187. msg_ctl |= (SPI_HD_R << SPI_MSG_TYPE_SHIFT);
  188. else if (t->tx_buf)
  189. msg_ctl |= (SPI_HD_W << SPI_MSG_TYPE_SHIFT);
  190. bcm_spi_writew(bs, msg_ctl, SPI_MSG_CTL);
  191. /* Issue the transfer */
  192. cmd = SPI_CMD_START_IMMEDIATE;
  193. cmd |= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
  194. cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT);
  195. bcm_spi_writew(bs, cmd, SPI_CMD);
  196. /* Enable the CMD_DONE interrupt */
  197. bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
  198. return t->len - bs->remaining_bytes;
  199. }
  200. static int bcm63xx_spi_prepare_transfer(struct spi_master *master)
  201. {
  202. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  203. pm_runtime_get_sync(&bs->pdev->dev);
  204. return 0;
  205. }
  206. static int bcm63xx_spi_unprepare_transfer(struct spi_master *master)
  207. {
  208. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  209. pm_runtime_put(&bs->pdev->dev);
  210. return 0;
  211. }
  212. static int bcm63xx_spi_transfer_one(struct spi_master *master,
  213. struct spi_message *m)
  214. {
  215. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  216. struct spi_transfer *t;
  217. struct spi_device *spi = m->spi;
  218. int status = 0;
  219. unsigned int timeout = 0;
  220. list_for_each_entry(t, &m->transfers, transfer_list) {
  221. unsigned int len = t->len;
  222. u8 rx_tail;
  223. status = bcm63xx_spi_check_transfer(spi, t);
  224. if (status < 0)
  225. goto exit;
  226. /* configure adapter for a new transfer */
  227. bcm63xx_spi_setup_transfer(spi, t);
  228. while (len) {
  229. /* send the data */
  230. len -= bcm63xx_txrx_bufs(spi, t);
  231. timeout = wait_for_completion_timeout(&bs->done, HZ);
  232. if (!timeout) {
  233. status = -ETIMEDOUT;
  234. goto exit;
  235. }
  236. /* read out all data */
  237. rx_tail = bcm_spi_readb(bs, SPI_RX_TAIL);
  238. /* Read out all the data */
  239. if (rx_tail)
  240. memcpy_fromio(bs->rx_ptr, bs->rx_io, rx_tail);
  241. }
  242. m->actual_length += t->len;
  243. }
  244. exit:
  245. m->status = status;
  246. spi_finalize_current_message(master);
  247. return 0;
  248. }
  249. /* This driver supports single master mode only. Hence
  250. * CMD_DONE is the only interrupt we care about
  251. */
  252. static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id)
  253. {
  254. struct spi_master *master = (struct spi_master *)dev_id;
  255. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  256. u8 intr;
  257. /* Read interupts and clear them immediately */
  258. intr = bcm_spi_readb(bs, SPI_INT_STATUS);
  259. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  260. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  261. /* A transfer completed */
  262. if (intr & SPI_INTR_CMD_DONE)
  263. complete(&bs->done);
  264. return IRQ_HANDLED;
  265. }
  266. static int __devinit bcm63xx_spi_probe(struct platform_device *pdev)
  267. {
  268. struct resource *r;
  269. struct device *dev = &pdev->dev;
  270. struct bcm63xx_spi_pdata *pdata = pdev->dev.platform_data;
  271. int irq;
  272. struct spi_master *master;
  273. struct clk *clk;
  274. struct bcm63xx_spi *bs;
  275. int ret;
  276. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  277. if (!r) {
  278. dev_err(dev, "no iomem\n");
  279. ret = -ENXIO;
  280. goto out;
  281. }
  282. irq = platform_get_irq(pdev, 0);
  283. if (irq < 0) {
  284. dev_err(dev, "no irq\n");
  285. ret = -ENXIO;
  286. goto out;
  287. }
  288. clk = clk_get(dev, "spi");
  289. if (IS_ERR(clk)) {
  290. dev_err(dev, "no clock for device\n");
  291. ret = PTR_ERR(clk);
  292. goto out;
  293. }
  294. master = spi_alloc_master(dev, sizeof(*bs));
  295. if (!master) {
  296. dev_err(dev, "out of memory\n");
  297. ret = -ENOMEM;
  298. goto out_clk;
  299. }
  300. bs = spi_master_get_devdata(master);
  301. platform_set_drvdata(pdev, master);
  302. bs->pdev = pdev;
  303. if (!devm_request_mem_region(&pdev->dev, r->start,
  304. resource_size(r), PFX)) {
  305. dev_err(dev, "iomem request failed\n");
  306. ret = -ENXIO;
  307. goto out_err;
  308. }
  309. bs->regs = devm_ioremap_nocache(&pdev->dev, r->start,
  310. resource_size(r));
  311. if (!bs->regs) {
  312. dev_err(dev, "unable to ioremap regs\n");
  313. ret = -ENOMEM;
  314. goto out_err;
  315. }
  316. bs->irq = irq;
  317. bs->clk = clk;
  318. bs->fifo_size = pdata->fifo_size;
  319. ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0,
  320. pdev->name, master);
  321. if (ret) {
  322. dev_err(dev, "unable to request irq\n");
  323. goto out_err;
  324. }
  325. master->bus_num = pdata->bus_num;
  326. master->num_chipselect = pdata->num_chipselect;
  327. master->setup = bcm63xx_spi_setup;
  328. master->prepare_transfer_hardware = bcm63xx_spi_prepare_transfer;
  329. master->unprepare_transfer_hardware = bcm63xx_spi_unprepare_transfer;
  330. master->transfer_one_message = bcm63xx_spi_transfer_one;
  331. bs->speed_hz = pdata->speed_hz;
  332. bs->stopping = 0;
  333. bs->tx_io = (u8 *)(bs->regs + bcm63xx_spireg(SPI_MSG_DATA));
  334. bs->rx_io = (const u8 *)(bs->regs + bcm63xx_spireg(SPI_RX_DATA));
  335. spin_lock_init(&bs->lock);
  336. /* Initialize hardware */
  337. clk_enable(bs->clk);
  338. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  339. /* register and we are done */
  340. ret = spi_register_master(master);
  341. if (ret) {
  342. dev_err(dev, "spi register failed\n");
  343. goto out_clk_disable;
  344. }
  345. dev_info(dev, "at 0x%08x (irq %d, FIFOs size %d) v%s\n",
  346. r->start, irq, bs->fifo_size, DRV_VER);
  347. return 0;
  348. out_clk_disable:
  349. clk_disable(clk);
  350. out_err:
  351. platform_set_drvdata(pdev, NULL);
  352. spi_master_put(master);
  353. out_clk:
  354. clk_put(clk);
  355. out:
  356. return ret;
  357. }
  358. static int __devexit bcm63xx_spi_remove(struct platform_device *pdev)
  359. {
  360. struct spi_master *master = platform_get_drvdata(pdev);
  361. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  362. /* reset spi block */
  363. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  364. spin_lock(&bs->lock);
  365. bs->stopping = 1;
  366. /* HW shutdown */
  367. clk_disable(bs->clk);
  368. clk_put(bs->clk);
  369. spin_unlock(&bs->lock);
  370. platform_set_drvdata(pdev, 0);
  371. spi_unregister_master(master);
  372. return 0;
  373. }
  374. #ifdef CONFIG_PM
  375. static int bcm63xx_spi_suspend(struct device *dev)
  376. {
  377. struct spi_master *master =
  378. platform_get_drvdata(to_platform_device(dev));
  379. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  380. clk_disable(bs->clk);
  381. return 0;
  382. }
  383. static int bcm63xx_spi_resume(struct device *dev)
  384. {
  385. struct spi_master *master =
  386. platform_get_drvdata(to_platform_device(dev));
  387. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  388. clk_enable(bs->clk);
  389. return 0;
  390. }
  391. static const struct dev_pm_ops bcm63xx_spi_pm_ops = {
  392. .suspend = bcm63xx_spi_suspend,
  393. .resume = bcm63xx_spi_resume,
  394. };
  395. #define BCM63XX_SPI_PM_OPS (&bcm63xx_spi_pm_ops)
  396. #else
  397. #define BCM63XX_SPI_PM_OPS NULL
  398. #endif
  399. static struct platform_driver bcm63xx_spi_driver = {
  400. .driver = {
  401. .name = "bcm63xx-spi",
  402. .owner = THIS_MODULE,
  403. .pm = BCM63XX_SPI_PM_OPS,
  404. },
  405. .probe = bcm63xx_spi_probe,
  406. .remove = __devexit_p(bcm63xx_spi_remove),
  407. };
  408. module_platform_driver(bcm63xx_spi_driver);
  409. MODULE_ALIAS("platform:bcm63xx_spi");
  410. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  411. MODULE_AUTHOR("Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>");
  412. MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
  413. MODULE_LICENSE("GPL");