spi-bcm63xx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. #include <linux/kernel.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/completion.h>
  26. #include <linux/err.h>
  27. #include <linux/pm_runtime.h>
  28. /* BCM 6338/6348 SPI core */
  29. #define SPI_6348_RSET_SIZE 64
  30. #define SPI_6348_CMD 0x00 /* 16-bits register */
  31. #define SPI_6348_INT_STATUS 0x02
  32. #define SPI_6348_INT_MASK_ST 0x03
  33. #define SPI_6348_INT_MASK 0x04
  34. #define SPI_6348_ST 0x05
  35. #define SPI_6348_CLK_CFG 0x06
  36. #define SPI_6348_FILL_BYTE 0x07
  37. #define SPI_6348_MSG_TAIL 0x09
  38. #define SPI_6348_RX_TAIL 0x0b
  39. #define SPI_6348_MSG_CTL 0x40 /* 8-bits register */
  40. #define SPI_6348_MSG_CTL_WIDTH 8
  41. #define SPI_6348_MSG_DATA 0x41
  42. #define SPI_6348_MSG_DATA_SIZE 0x3f
  43. #define SPI_6348_RX_DATA 0x80
  44. #define SPI_6348_RX_DATA_SIZE 0x3f
  45. /* BCM 3368/6358/6262/6368 SPI core */
  46. #define SPI_6358_RSET_SIZE 1804
  47. #define SPI_6358_MSG_CTL 0x00 /* 16-bits register */
  48. #define SPI_6358_MSG_CTL_WIDTH 16
  49. #define SPI_6358_MSG_DATA 0x02
  50. #define SPI_6358_MSG_DATA_SIZE 0x21e
  51. #define SPI_6358_RX_DATA 0x400
  52. #define SPI_6358_RX_DATA_SIZE 0x220
  53. #define SPI_6358_CMD 0x700 /* 16-bits register */
  54. #define SPI_6358_INT_STATUS 0x702
  55. #define SPI_6358_INT_MASK_ST 0x703
  56. #define SPI_6358_INT_MASK 0x704
  57. #define SPI_6358_ST 0x705
  58. #define SPI_6358_CLK_CFG 0x706
  59. #define SPI_6358_FILL_BYTE 0x707
  60. #define SPI_6358_MSG_TAIL 0x709
  61. #define SPI_6358_RX_TAIL 0x70B
  62. /* Shared SPI definitions */
  63. /* Message configuration */
  64. #define SPI_FD_RW 0x00
  65. #define SPI_HD_W 0x01
  66. #define SPI_HD_R 0x02
  67. #define SPI_BYTE_CNT_SHIFT 0
  68. #define SPI_6348_MSG_TYPE_SHIFT 6
  69. #define SPI_6358_MSG_TYPE_SHIFT 14
  70. /* Command */
  71. #define SPI_CMD_NOOP 0x00
  72. #define SPI_CMD_SOFT_RESET 0x01
  73. #define SPI_CMD_HARD_RESET 0x02
  74. #define SPI_CMD_START_IMMEDIATE 0x03
  75. #define SPI_CMD_COMMAND_SHIFT 0
  76. #define SPI_CMD_COMMAND_MASK 0x000f
  77. #define SPI_CMD_DEVICE_ID_SHIFT 4
  78. #define SPI_CMD_PREPEND_BYTE_CNT_SHIFT 8
  79. #define SPI_CMD_ONE_BYTE_SHIFT 11
  80. #define SPI_CMD_ONE_WIRE_SHIFT 12
  81. #define SPI_DEV_ID_0 0
  82. #define SPI_DEV_ID_1 1
  83. #define SPI_DEV_ID_2 2
  84. #define SPI_DEV_ID_3 3
  85. /* Interrupt mask */
  86. #define SPI_INTR_CMD_DONE 0x01
  87. #define SPI_INTR_RX_OVERFLOW 0x02
  88. #define SPI_INTR_TX_UNDERFLOW 0x04
  89. #define SPI_INTR_TX_OVERFLOW 0x08
  90. #define SPI_INTR_RX_UNDERFLOW 0x10
  91. #define SPI_INTR_CLEAR_ALL 0x1f
  92. /* Status */
  93. #define SPI_RX_EMPTY 0x02
  94. #define SPI_CMD_BUSY 0x04
  95. #define SPI_SERIAL_BUSY 0x08
  96. /* Clock configuration */
  97. #define SPI_CLK_20MHZ 0x00
  98. #define SPI_CLK_0_391MHZ 0x01
  99. #define SPI_CLK_0_781MHZ 0x02 /* default */
  100. #define SPI_CLK_1_563MHZ 0x03
  101. #define SPI_CLK_3_125MHZ 0x04
  102. #define SPI_CLK_6_250MHZ 0x05
  103. #define SPI_CLK_12_50MHZ 0x06
  104. #define SPI_CLK_MASK 0x07
  105. #define SPI_SSOFFTIME_MASK 0x38
  106. #define SPI_SSOFFTIME_SHIFT 3
  107. #define SPI_BYTE_SWAP 0x80
  108. enum bcm63xx_regs_spi {
  109. SPI_CMD,
  110. SPI_INT_STATUS,
  111. SPI_INT_MASK_ST,
  112. SPI_INT_MASK,
  113. SPI_ST,
  114. SPI_CLK_CFG,
  115. SPI_FILL_BYTE,
  116. SPI_MSG_TAIL,
  117. SPI_RX_TAIL,
  118. SPI_MSG_CTL,
  119. SPI_MSG_DATA,
  120. SPI_RX_DATA,
  121. SPI_MSG_TYPE_SHIFT,
  122. SPI_MSG_CTL_WIDTH,
  123. SPI_MSG_DATA_SIZE,
  124. };
  125. #define BCM63XX_SPI_MAX_PREPEND 15
  126. #define BCM63XX_SPI_MAX_CS 8
  127. #define BCM63XX_SPI_BUS_NUM 0
  128. struct bcm63xx_spi {
  129. struct completion done;
  130. void __iomem *regs;
  131. int irq;
  132. /* Platform data */
  133. const unsigned long *reg_offsets;
  134. unsigned fifo_size;
  135. unsigned int msg_type_shift;
  136. unsigned int msg_ctl_width;
  137. /* data iomem */
  138. u8 __iomem *tx_io;
  139. const u8 __iomem *rx_io;
  140. struct clk *clk;
  141. struct platform_device *pdev;
  142. };
  143. static inline u8 bcm_spi_readb(struct bcm63xx_spi *bs,
  144. unsigned int offset)
  145. {
  146. return readb(bs->regs + bs->reg_offsets[offset]);
  147. }
  148. static inline u16 bcm_spi_readw(struct bcm63xx_spi *bs,
  149. unsigned int offset)
  150. {
  151. #ifdef CONFIG_CPU_BIG_ENDIAN
  152. return ioread16be(bs->regs + bs->reg_offsets[offset]);
  153. #else
  154. return readw(bs->regs + bs->reg_offsets[offset]);
  155. #endif
  156. }
  157. static inline void bcm_spi_writeb(struct bcm63xx_spi *bs,
  158. u8 value, unsigned int offset)
  159. {
  160. writeb(value, bs->regs + bs->reg_offsets[offset]);
  161. }
  162. static inline void bcm_spi_writew(struct bcm63xx_spi *bs,
  163. u16 value, unsigned int offset)
  164. {
  165. #ifdef CONFIG_CPU_BIG_ENDIAN
  166. iowrite16be(value, bs->regs + bs->reg_offsets[offset]);
  167. #else
  168. writew(value, bs->regs + bs->reg_offsets[offset]);
  169. #endif
  170. }
  171. static const unsigned bcm63xx_spi_freq_table[SPI_CLK_MASK][2] = {
  172. { 20000000, SPI_CLK_20MHZ },
  173. { 12500000, SPI_CLK_12_50MHZ },
  174. { 6250000, SPI_CLK_6_250MHZ },
  175. { 3125000, SPI_CLK_3_125MHZ },
  176. { 1563000, SPI_CLK_1_563MHZ },
  177. { 781000, SPI_CLK_0_781MHZ },
  178. { 391000, SPI_CLK_0_391MHZ }
  179. };
  180. static void bcm63xx_spi_setup_transfer(struct spi_device *spi,
  181. struct spi_transfer *t)
  182. {
  183. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  184. u8 clk_cfg, reg;
  185. int i;
  186. /* Find the closest clock configuration */
  187. for (i = 0; i < SPI_CLK_MASK; i++) {
  188. if (t->speed_hz >= bcm63xx_spi_freq_table[i][0]) {
  189. clk_cfg = bcm63xx_spi_freq_table[i][1];
  190. break;
  191. }
  192. }
  193. /* No matching configuration found, default to lowest */
  194. if (i == SPI_CLK_MASK)
  195. clk_cfg = SPI_CLK_0_391MHZ;
  196. /* clear existing clock configuration bits of the register */
  197. reg = bcm_spi_readb(bs, SPI_CLK_CFG);
  198. reg &= ~SPI_CLK_MASK;
  199. reg |= clk_cfg;
  200. bcm_spi_writeb(bs, reg, SPI_CLK_CFG);
  201. dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n",
  202. clk_cfg, t->speed_hz);
  203. }
  204. /* the spi->mode bits understood by this driver: */
  205. #define MODEBITS (SPI_CPOL | SPI_CPHA)
  206. static int bcm63xx_txrx_bufs(struct spi_device *spi, struct spi_transfer *first,
  207. unsigned int num_transfers)
  208. {
  209. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  210. u16 msg_ctl;
  211. u16 cmd;
  212. unsigned int i, timeout = 0, prepend_len = 0, len = 0;
  213. struct spi_transfer *t = first;
  214. bool do_rx = false;
  215. bool do_tx = false;
  216. /* Disable the CMD_DONE interrupt */
  217. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  218. dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
  219. t->tx_buf, t->rx_buf, t->len);
  220. if (num_transfers > 1 && t->tx_buf && t->len <= BCM63XX_SPI_MAX_PREPEND)
  221. prepend_len = t->len;
  222. /* prepare the buffer */
  223. for (i = 0; i < num_transfers; i++) {
  224. if (t->tx_buf) {
  225. do_tx = true;
  226. memcpy_toio(bs->tx_io + len, t->tx_buf, t->len);
  227. /* don't prepend more than one tx */
  228. if (t != first)
  229. prepend_len = 0;
  230. }
  231. if (t->rx_buf) {
  232. do_rx = true;
  233. /* prepend is half-duplex write only */
  234. if (t == first)
  235. prepend_len = 0;
  236. }
  237. len += t->len;
  238. t = list_entry(t->transfer_list.next, struct spi_transfer,
  239. transfer_list);
  240. }
  241. reinit_completion(&bs->done);
  242. /* Fill in the Message control register */
  243. msg_ctl = (len << SPI_BYTE_CNT_SHIFT);
  244. if (do_rx && do_tx && prepend_len == 0)
  245. msg_ctl |= (SPI_FD_RW << bs->msg_type_shift);
  246. else if (do_rx)
  247. msg_ctl |= (SPI_HD_R << bs->msg_type_shift);
  248. else if (do_tx)
  249. msg_ctl |= (SPI_HD_W << bs->msg_type_shift);
  250. switch (bs->msg_ctl_width) {
  251. case 8:
  252. bcm_spi_writeb(bs, msg_ctl, SPI_MSG_CTL);
  253. break;
  254. case 16:
  255. bcm_spi_writew(bs, msg_ctl, SPI_MSG_CTL);
  256. break;
  257. }
  258. /* Issue the transfer */
  259. cmd = SPI_CMD_START_IMMEDIATE;
  260. cmd |= (prepend_len << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
  261. cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT);
  262. bcm_spi_writew(bs, cmd, SPI_CMD);
  263. /* Enable the CMD_DONE interrupt */
  264. bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
  265. timeout = wait_for_completion_timeout(&bs->done, HZ);
  266. if (!timeout)
  267. return -ETIMEDOUT;
  268. if (!do_rx)
  269. return 0;
  270. len = 0;
  271. t = first;
  272. /* Read out all the data */
  273. for (i = 0; i < num_transfers; i++) {
  274. if (t->rx_buf)
  275. memcpy_fromio(t->rx_buf, bs->rx_io + len, t->len);
  276. if (t != first || prepend_len == 0)
  277. len += t->len;
  278. t = list_entry(t->transfer_list.next, struct spi_transfer,
  279. transfer_list);
  280. }
  281. return 0;
  282. }
  283. static int bcm63xx_spi_transfer_one(struct spi_master *master,
  284. struct spi_message *m)
  285. {
  286. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  287. struct spi_transfer *t, *first = NULL;
  288. struct spi_device *spi = m->spi;
  289. int status = 0;
  290. unsigned int n_transfers = 0, total_len = 0;
  291. bool can_use_prepend = false;
  292. /*
  293. * This SPI controller does not support keeping CS active after a
  294. * transfer.
  295. * Work around this by merging as many transfers we can into one big
  296. * full-duplex transfers.
  297. */
  298. list_for_each_entry(t, &m->transfers, transfer_list) {
  299. if (!first)
  300. first = t;
  301. n_transfers++;
  302. total_len += t->len;
  303. if (n_transfers == 2 && !first->rx_buf && !t->tx_buf &&
  304. first->len <= BCM63XX_SPI_MAX_PREPEND)
  305. can_use_prepend = true;
  306. else if (can_use_prepend && t->tx_buf)
  307. can_use_prepend = false;
  308. /* we can only transfer one fifo worth of data */
  309. if ((can_use_prepend &&
  310. total_len > (bs->fifo_size + BCM63XX_SPI_MAX_PREPEND)) ||
  311. (!can_use_prepend && total_len > bs->fifo_size)) {
  312. dev_err(&spi->dev, "unable to do transfers larger than FIFO size (%i > %i)\n",
  313. total_len, bs->fifo_size);
  314. status = -EINVAL;
  315. goto exit;
  316. }
  317. /* all combined transfers have to have the same speed */
  318. if (t->speed_hz != first->speed_hz) {
  319. dev_err(&spi->dev, "unable to change speed between transfers\n");
  320. status = -EINVAL;
  321. goto exit;
  322. }
  323. /* CS will be deasserted directly after transfer */
  324. if (t->delay_usecs) {
  325. dev_err(&spi->dev, "unable to keep CS asserted after transfer\n");
  326. status = -EINVAL;
  327. goto exit;
  328. }
  329. if (t->cs_change ||
  330. list_is_last(&t->transfer_list, &m->transfers)) {
  331. /* configure adapter for a new transfer */
  332. bcm63xx_spi_setup_transfer(spi, first);
  333. /* send the data */
  334. status = bcm63xx_txrx_bufs(spi, first, n_transfers);
  335. if (status)
  336. goto exit;
  337. m->actual_length += total_len;
  338. first = NULL;
  339. n_transfers = 0;
  340. total_len = 0;
  341. can_use_prepend = false;
  342. }
  343. }
  344. exit:
  345. m->status = status;
  346. spi_finalize_current_message(master);
  347. return 0;
  348. }
  349. /* This driver supports single master mode only. Hence
  350. * CMD_DONE is the only interrupt we care about
  351. */
  352. static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id)
  353. {
  354. struct spi_master *master = (struct spi_master *)dev_id;
  355. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  356. u8 intr;
  357. /* Read interupts and clear them immediately */
  358. intr = bcm_spi_readb(bs, SPI_INT_STATUS);
  359. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  360. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  361. /* A transfer completed */
  362. if (intr & SPI_INTR_CMD_DONE)
  363. complete(&bs->done);
  364. return IRQ_HANDLED;
  365. }
  366. static const unsigned long bcm6348_spi_reg_offsets[] = {
  367. [SPI_CMD] = SPI_6348_CMD,
  368. [SPI_INT_STATUS] = SPI_6348_INT_STATUS,
  369. [SPI_INT_MASK_ST] = SPI_6348_INT_MASK_ST,
  370. [SPI_INT_MASK] = SPI_6348_INT_MASK,
  371. [SPI_ST] = SPI_6348_ST,
  372. [SPI_CLK_CFG] = SPI_6348_CLK_CFG,
  373. [SPI_FILL_BYTE] = SPI_6348_FILL_BYTE,
  374. [SPI_MSG_TAIL] = SPI_6348_MSG_TAIL,
  375. [SPI_RX_TAIL] = SPI_6348_RX_TAIL,
  376. [SPI_MSG_CTL] = SPI_6348_MSG_CTL,
  377. [SPI_MSG_DATA] = SPI_6348_MSG_DATA,
  378. [SPI_RX_DATA] = SPI_6348_RX_DATA,
  379. [SPI_MSG_TYPE_SHIFT] = SPI_6348_MSG_TYPE_SHIFT,
  380. [SPI_MSG_CTL_WIDTH] = SPI_6348_MSG_CTL_WIDTH,
  381. [SPI_MSG_DATA_SIZE] = SPI_6348_MSG_DATA_SIZE,
  382. };
  383. static const unsigned long bcm6358_spi_reg_offsets[] = {
  384. [SPI_CMD] = SPI_6358_CMD,
  385. [SPI_INT_STATUS] = SPI_6358_INT_STATUS,
  386. [SPI_INT_MASK_ST] = SPI_6358_INT_MASK_ST,
  387. [SPI_INT_MASK] = SPI_6358_INT_MASK,
  388. [SPI_ST] = SPI_6358_ST,
  389. [SPI_CLK_CFG] = SPI_6358_CLK_CFG,
  390. [SPI_FILL_BYTE] = SPI_6358_FILL_BYTE,
  391. [SPI_MSG_TAIL] = SPI_6358_MSG_TAIL,
  392. [SPI_RX_TAIL] = SPI_6358_RX_TAIL,
  393. [SPI_MSG_CTL] = SPI_6358_MSG_CTL,
  394. [SPI_MSG_DATA] = SPI_6358_MSG_DATA,
  395. [SPI_RX_DATA] = SPI_6358_RX_DATA,
  396. [SPI_MSG_TYPE_SHIFT] = SPI_6358_MSG_TYPE_SHIFT,
  397. [SPI_MSG_CTL_WIDTH] = SPI_6358_MSG_CTL_WIDTH,
  398. [SPI_MSG_DATA_SIZE] = SPI_6358_MSG_DATA_SIZE,
  399. };
  400. static const struct platform_device_id bcm63xx_spi_dev_match[] = {
  401. {
  402. .name = "bcm6348-spi",
  403. .driver_data = (unsigned long)bcm6348_spi_reg_offsets,
  404. },
  405. {
  406. .name = "bcm6358-spi",
  407. .driver_data = (unsigned long)bcm6358_spi_reg_offsets,
  408. },
  409. {
  410. },
  411. };
  412. static int bcm63xx_spi_probe(struct platform_device *pdev)
  413. {
  414. struct resource *r;
  415. const unsigned long *bcm63xx_spireg;
  416. struct device *dev = &pdev->dev;
  417. int irq;
  418. struct spi_master *master;
  419. struct clk *clk;
  420. struct bcm63xx_spi *bs;
  421. int ret;
  422. if (!pdev->id_entry->driver_data)
  423. return -EINVAL;
  424. bcm63xx_spireg = (const unsigned long *)pdev->id_entry->driver_data;
  425. irq = platform_get_irq(pdev, 0);
  426. if (irq < 0) {
  427. dev_err(dev, "no irq: %d\n", irq);
  428. return irq;
  429. }
  430. clk = devm_clk_get(dev, "spi");
  431. if (IS_ERR(clk)) {
  432. dev_err(dev, "no clock for device\n");
  433. return PTR_ERR(clk);
  434. }
  435. master = spi_alloc_master(dev, sizeof(*bs));
  436. if (!master) {
  437. dev_err(dev, "out of memory\n");
  438. return -ENOMEM;
  439. }
  440. bs = spi_master_get_devdata(master);
  441. init_completion(&bs->done);
  442. platform_set_drvdata(pdev, master);
  443. bs->pdev = pdev;
  444. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  445. bs->regs = devm_ioremap_resource(&pdev->dev, r);
  446. if (IS_ERR(bs->regs)) {
  447. ret = PTR_ERR(bs->regs);
  448. goto out_err;
  449. }
  450. bs->irq = irq;
  451. bs->clk = clk;
  452. bs->reg_offsets = bcm63xx_spireg;
  453. bs->fifo_size = bs->reg_offsets[SPI_MSG_DATA_SIZE];
  454. ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0,
  455. pdev->name, master);
  456. if (ret) {
  457. dev_err(dev, "unable to request irq\n");
  458. goto out_err;
  459. }
  460. master->bus_num = BCM63XX_SPI_BUS_NUM;
  461. master->num_chipselect = BCM63XX_SPI_MAX_CS;
  462. master->transfer_one_message = bcm63xx_spi_transfer_one;
  463. master->mode_bits = MODEBITS;
  464. master->bits_per_word_mask = SPI_BPW_MASK(8);
  465. master->auto_runtime_pm = true;
  466. bs->msg_type_shift = bs->reg_offsets[SPI_MSG_TYPE_SHIFT];
  467. bs->msg_ctl_width = bs->reg_offsets[SPI_MSG_CTL_WIDTH];
  468. bs->tx_io = (u8 *)(bs->regs + bs->reg_offsets[SPI_MSG_DATA]);
  469. bs->rx_io = (const u8 *)(bs->regs + bs->reg_offsets[SPI_RX_DATA]);
  470. /* Initialize hardware */
  471. ret = clk_prepare_enable(bs->clk);
  472. if (ret)
  473. goto out_err;
  474. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  475. /* register and we are done */
  476. ret = devm_spi_register_master(dev, master);
  477. if (ret) {
  478. dev_err(dev, "spi register failed\n");
  479. goto out_clk_disable;
  480. }
  481. dev_info(dev, "at %pr (irq %d, FIFOs size %d)\n",
  482. r, irq, bs->fifo_size);
  483. return 0;
  484. out_clk_disable:
  485. clk_disable_unprepare(clk);
  486. out_err:
  487. spi_master_put(master);
  488. return ret;
  489. }
  490. static int bcm63xx_spi_remove(struct platform_device *pdev)
  491. {
  492. struct spi_master *master = platform_get_drvdata(pdev);
  493. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  494. /* reset spi block */
  495. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  496. /* HW shutdown */
  497. clk_disable_unprepare(bs->clk);
  498. return 0;
  499. }
  500. #ifdef CONFIG_PM_SLEEP
  501. static int bcm63xx_spi_suspend(struct device *dev)
  502. {
  503. struct spi_master *master = dev_get_drvdata(dev);
  504. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  505. spi_master_suspend(master);
  506. clk_disable_unprepare(bs->clk);
  507. return 0;
  508. }
  509. static int bcm63xx_spi_resume(struct device *dev)
  510. {
  511. struct spi_master *master = dev_get_drvdata(dev);
  512. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  513. int ret;
  514. ret = clk_prepare_enable(bs->clk);
  515. if (ret)
  516. return ret;
  517. spi_master_resume(master);
  518. return 0;
  519. }
  520. #endif
  521. static const struct dev_pm_ops bcm63xx_spi_pm_ops = {
  522. SET_SYSTEM_SLEEP_PM_OPS(bcm63xx_spi_suspend, bcm63xx_spi_resume)
  523. };
  524. static struct platform_driver bcm63xx_spi_driver = {
  525. .driver = {
  526. .name = "bcm63xx-spi",
  527. .pm = &bcm63xx_spi_pm_ops,
  528. },
  529. .id_table = bcm63xx_spi_dev_match,
  530. .probe = bcm63xx_spi_probe,
  531. .remove = bcm63xx_spi_remove,
  532. };
  533. module_platform_driver(bcm63xx_spi_driver);
  534. MODULE_ALIAS("platform:bcm63xx_spi");
  535. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  536. MODULE_AUTHOR("Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>");
  537. MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
  538. MODULE_LICENSE("GPL");