spi-s3c24xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2006 Ben Dooks
  4. * Copyright 2006-2009 Simtec Electronics
  5. * Ben Dooks <[email protected]>
  6. */
  7. #include <linux/spinlock.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/delay.h>
  10. #include <linux/errno.h>
  11. #include <linux/err.h>
  12. #include <linux/clk.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/spi/spi_bitbang.h>
  18. #include <linux/spi/s3c24xx.h>
  19. #include <linux/spi/s3c24xx-fiq.h>
  20. #include <linux/module.h>
  21. #include <asm/fiq.h>
  22. #include "spi-s3c24xx-regs.h"
  23. /**
  24. * struct s3c24xx_spi_devstate - per device data
  25. * @hz: Last frequency calculated for @sppre field.
  26. * @mode: Last mode setting for the @spcon field.
  27. * @spcon: Value to write to the SPCON register.
  28. * @sppre: Value to write to the SPPRE register.
  29. */
  30. struct s3c24xx_spi_devstate {
  31. unsigned int hz;
  32. unsigned int mode;
  33. u8 spcon;
  34. u8 sppre;
  35. };
  36. enum spi_fiq_mode {
  37. FIQ_MODE_NONE = 0,
  38. FIQ_MODE_TX = 1,
  39. FIQ_MODE_RX = 2,
  40. FIQ_MODE_TXRX = 3,
  41. };
  42. struct s3c24xx_spi {
  43. /* bitbang has to be first */
  44. struct spi_bitbang bitbang;
  45. struct completion done;
  46. void __iomem *regs;
  47. int irq;
  48. int len;
  49. int count;
  50. struct fiq_handler fiq_handler;
  51. enum spi_fiq_mode fiq_mode;
  52. unsigned char fiq_inuse;
  53. unsigned char fiq_claimed;
  54. /* data buffers */
  55. const unsigned char *tx;
  56. unsigned char *rx;
  57. struct clk *clk;
  58. struct spi_master *master;
  59. struct spi_device *curdev;
  60. struct device *dev;
  61. struct s3c2410_spi_info *pdata;
  62. };
  63. #define SPCON_DEFAULT (S3C2410_SPCON_MSTR | S3C2410_SPCON_SMOD_INT)
  64. #define SPPIN_DEFAULT (S3C2410_SPPIN_KEEP)
  65. static inline struct s3c24xx_spi *to_hw(struct spi_device *sdev)
  66. {
  67. return spi_master_get_devdata(sdev->master);
  68. }
  69. static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)
  70. {
  71. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  72. struct s3c24xx_spi *hw = to_hw(spi);
  73. /* change the chipselect state and the state of the spi engine clock */
  74. switch (value) {
  75. case BITBANG_CS_INACTIVE:
  76. writeb(cs->spcon, hw->regs + S3C2410_SPCON);
  77. break;
  78. case BITBANG_CS_ACTIVE:
  79. writeb(cs->spcon | S3C2410_SPCON_ENSCK,
  80. hw->regs + S3C2410_SPCON);
  81. break;
  82. }
  83. }
  84. static int s3c24xx_spi_update_state(struct spi_device *spi,
  85. struct spi_transfer *t)
  86. {
  87. struct s3c24xx_spi *hw = to_hw(spi);
  88. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  89. unsigned int hz;
  90. unsigned int div;
  91. unsigned long clk;
  92. hz = t ? t->speed_hz : spi->max_speed_hz;
  93. if (!hz)
  94. hz = spi->max_speed_hz;
  95. if (spi->mode != cs->mode) {
  96. u8 spcon = SPCON_DEFAULT | S3C2410_SPCON_ENSCK;
  97. if (spi->mode & SPI_CPHA)
  98. spcon |= S3C2410_SPCON_CPHA_FMTB;
  99. if (spi->mode & SPI_CPOL)
  100. spcon |= S3C2410_SPCON_CPOL_HIGH;
  101. cs->mode = spi->mode;
  102. cs->spcon = spcon;
  103. }
  104. if (cs->hz != hz) {
  105. clk = clk_get_rate(hw->clk);
  106. div = DIV_ROUND_UP(clk, hz * 2) - 1;
  107. if (div > 255)
  108. div = 255;
  109. dev_dbg(&spi->dev, "pre-scaler=%d (wanted %d, got %ld)\n",
  110. div, hz, clk / (2 * (div + 1)));
  111. cs->hz = hz;
  112. cs->sppre = div;
  113. }
  114. return 0;
  115. }
  116. static int s3c24xx_spi_setupxfer(struct spi_device *spi,
  117. struct spi_transfer *t)
  118. {
  119. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  120. struct s3c24xx_spi *hw = to_hw(spi);
  121. int ret;
  122. ret = s3c24xx_spi_update_state(spi, t);
  123. if (!ret)
  124. writeb(cs->sppre, hw->regs + S3C2410_SPPRE);
  125. return ret;
  126. }
  127. static int s3c24xx_spi_setup(struct spi_device *spi)
  128. {
  129. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  130. struct s3c24xx_spi *hw = to_hw(spi);
  131. int ret;
  132. /* allocate settings on the first call */
  133. if (!cs) {
  134. cs = devm_kzalloc(&spi->dev,
  135. sizeof(struct s3c24xx_spi_devstate),
  136. GFP_KERNEL);
  137. if (!cs)
  138. return -ENOMEM;
  139. cs->spcon = SPCON_DEFAULT;
  140. cs->hz = -1;
  141. spi->controller_state = cs;
  142. }
  143. /* initialise the state from the device */
  144. ret = s3c24xx_spi_update_state(spi, NULL);
  145. if (ret)
  146. return ret;
  147. mutex_lock(&hw->bitbang.lock);
  148. if (!hw->bitbang.busy) {
  149. hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
  150. /* need to ndelay for 0.5 clocktick ? */
  151. }
  152. mutex_unlock(&hw->bitbang.lock);
  153. return 0;
  154. }
  155. static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)
  156. {
  157. return hw->tx ? hw->tx[count] : 0;
  158. }
  159. #ifdef CONFIG_SPI_S3C24XX_FIQ
  160. /* Support for FIQ based pseudo-DMA to improve the transfer speed.
  161. *
  162. * This code uses the assembly helper in spi_s3c24xx_spi.S which is
  163. * used by the FIQ core to move data between main memory and the peripheral
  164. * block. Since this is code running on the processor, there is no problem
  165. * with cache coherency of the buffers, so we can use any buffer we like.
  166. */
  167. /**
  168. * struct spi_fiq_code - FIQ code and header
  169. * @length: The length of the code fragment, excluding this header.
  170. * @ack_offset: The offset from @data to the word to place the IRQ ACK bit at.
  171. * @data: The code itself to install as a FIQ handler.
  172. */
  173. struct spi_fiq_code {
  174. u32 length;
  175. u32 ack_offset;
  176. u8 data[];
  177. };
  178. /**
  179. * s3c24xx_spi_tryfiq - attempt to claim and setup FIQ for transfer
  180. * @hw: The hardware state.
  181. *
  182. * Claim the FIQ handler (only one can be active at any one time) and
  183. * then setup the correct transfer code for this transfer.
  184. *
  185. * This call updates all the necessary state information if successful,
  186. * so the caller does not need to do anything more than start the transfer
  187. * as normal, since the IRQ will have been re-routed to the FIQ handler.
  188. */
  189. static void s3c24xx_spi_tryfiq(struct s3c24xx_spi *hw)
  190. {
  191. struct pt_regs regs;
  192. enum spi_fiq_mode mode;
  193. struct spi_fiq_code *code;
  194. u32 *ack_ptr = NULL;
  195. int ret;
  196. if (!hw->fiq_claimed) {
  197. /* try and claim fiq if we haven't got it, and if not
  198. * then return and simply use another transfer method */
  199. ret = claim_fiq(&hw->fiq_handler);
  200. if (ret)
  201. return;
  202. }
  203. if (hw->tx && !hw->rx)
  204. mode = FIQ_MODE_TX;
  205. else if (hw->rx && !hw->tx)
  206. mode = FIQ_MODE_RX;
  207. else
  208. mode = FIQ_MODE_TXRX;
  209. regs.uregs[fiq_rspi] = (long)hw->regs;
  210. regs.uregs[fiq_rrx] = (long)hw->rx;
  211. regs.uregs[fiq_rtx] = (long)hw->tx + 1;
  212. regs.uregs[fiq_rcount] = hw->len - 1;
  213. set_fiq_regs(&regs);
  214. if (hw->fiq_mode != mode) {
  215. hw->fiq_mode = mode;
  216. switch (mode) {
  217. case FIQ_MODE_TX:
  218. code = &s3c24xx_spi_fiq_tx;
  219. break;
  220. case FIQ_MODE_RX:
  221. code = &s3c24xx_spi_fiq_rx;
  222. break;
  223. case FIQ_MODE_TXRX:
  224. code = &s3c24xx_spi_fiq_txrx;
  225. break;
  226. default:
  227. code = NULL;
  228. }
  229. BUG_ON(!code);
  230. ack_ptr = (u32 *)&code->data[code->ack_offset];
  231. set_fiq_handler(&code->data, code->length);
  232. }
  233. s3c24xx_set_fiq(hw->irq, ack_ptr, true);
  234. hw->fiq_mode = mode;
  235. hw->fiq_inuse = 1;
  236. }
  237. /**
  238. * s3c24xx_spi_fiqop - FIQ core code callback
  239. * @pw: Data registered with the handler
  240. * @release: Whether this is a release or a return.
  241. *
  242. * Called by the FIQ code when another module wants to use the FIQ, so
  243. * return whether we are currently using this or not and then update our
  244. * internal state.
  245. */
  246. static int s3c24xx_spi_fiqop(void *pw, int release)
  247. {
  248. struct s3c24xx_spi *hw = pw;
  249. int ret = 0;
  250. if (release) {
  251. if (hw->fiq_inuse)
  252. ret = -EBUSY;
  253. /* note, we do not need to unroute the FIQ, as the FIQ
  254. * vector code de-routes it to signal the end of transfer */
  255. hw->fiq_mode = FIQ_MODE_NONE;
  256. hw->fiq_claimed = 0;
  257. } else {
  258. hw->fiq_claimed = 1;
  259. }
  260. return ret;
  261. }
  262. /**
  263. * s3c24xx_spi_initfiq - setup the information for the FIQ core
  264. * @hw: The hardware state.
  265. *
  266. * Setup the fiq_handler block to pass to the FIQ core.
  267. */
  268. static inline void s3c24xx_spi_initfiq(struct s3c24xx_spi *hw)
  269. {
  270. hw->fiq_handler.dev_id = hw;
  271. hw->fiq_handler.name = dev_name(hw->dev);
  272. hw->fiq_handler.fiq_op = s3c24xx_spi_fiqop;
  273. }
  274. /**
  275. * s3c24xx_spi_usefiq - return if we should be using FIQ.
  276. * @hw: The hardware state.
  277. *
  278. * Return true if the platform data specifies whether this channel is
  279. * allowed to use the FIQ.
  280. */
  281. static inline bool s3c24xx_spi_usefiq(struct s3c24xx_spi *hw)
  282. {
  283. return hw->pdata->use_fiq;
  284. }
  285. /**
  286. * s3c24xx_spi_usingfiq - return if channel is using FIQ
  287. * @spi: The hardware state.
  288. *
  289. * Return whether the channel is currently using the FIQ (separate from
  290. * whether the FIQ is claimed).
  291. */
  292. static inline bool s3c24xx_spi_usingfiq(struct s3c24xx_spi *spi)
  293. {
  294. return spi->fiq_inuse;
  295. }
  296. #else
  297. static inline void s3c24xx_spi_initfiq(struct s3c24xx_spi *s) { }
  298. static inline void s3c24xx_spi_tryfiq(struct s3c24xx_spi *s) { }
  299. static inline bool s3c24xx_spi_usefiq(struct s3c24xx_spi *s) { return false; }
  300. static inline bool s3c24xx_spi_usingfiq(struct s3c24xx_spi *s) { return false; }
  301. #endif /* CONFIG_SPI_S3C24XX_FIQ */
  302. static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  303. {
  304. struct s3c24xx_spi *hw = to_hw(spi);
  305. hw->tx = t->tx_buf;
  306. hw->rx = t->rx_buf;
  307. hw->len = t->len;
  308. hw->count = 0;
  309. init_completion(&hw->done);
  310. hw->fiq_inuse = 0;
  311. if (s3c24xx_spi_usefiq(hw) && t->len >= 3)
  312. s3c24xx_spi_tryfiq(hw);
  313. /* send the first byte */
  314. writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);
  315. wait_for_completion(&hw->done);
  316. return hw->count;
  317. }
  318. static irqreturn_t s3c24xx_spi_irq(int irq, void *dev)
  319. {
  320. struct s3c24xx_spi *hw = dev;
  321. unsigned int spsta = readb(hw->regs + S3C2410_SPSTA);
  322. unsigned int count = hw->count;
  323. if (spsta & S3C2410_SPSTA_DCOL) {
  324. dev_dbg(hw->dev, "data-collision\n");
  325. complete(&hw->done);
  326. goto irq_done;
  327. }
  328. if (!(spsta & S3C2410_SPSTA_READY)) {
  329. dev_dbg(hw->dev, "spi not ready for tx?\n");
  330. complete(&hw->done);
  331. goto irq_done;
  332. }
  333. if (!s3c24xx_spi_usingfiq(hw)) {
  334. hw->count++;
  335. if (hw->rx)
  336. hw->rx[count] = readb(hw->regs + S3C2410_SPRDAT);
  337. count++;
  338. if (count < hw->len)
  339. writeb(hw_txbyte(hw, count), hw->regs + S3C2410_SPTDAT);
  340. else
  341. complete(&hw->done);
  342. } else {
  343. hw->count = hw->len;
  344. hw->fiq_inuse = 0;
  345. if (hw->rx)
  346. hw->rx[hw->len-1] = readb(hw->regs + S3C2410_SPRDAT);
  347. complete(&hw->done);
  348. }
  349. irq_done:
  350. return IRQ_HANDLED;
  351. }
  352. static void s3c24xx_spi_initialsetup(struct s3c24xx_spi *hw)
  353. {
  354. /* for the moment, permanently enable the clock */
  355. clk_enable(hw->clk);
  356. /* program defaults into the registers */
  357. writeb(0xff, hw->regs + S3C2410_SPPRE);
  358. writeb(SPPIN_DEFAULT, hw->regs + S3C2410_SPPIN);
  359. writeb(SPCON_DEFAULT, hw->regs + S3C2410_SPCON);
  360. }
  361. static int s3c24xx_spi_probe(struct platform_device *pdev)
  362. {
  363. struct s3c2410_spi_info *pdata;
  364. struct s3c24xx_spi *hw;
  365. struct spi_master *master;
  366. int err = 0;
  367. master = devm_spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));
  368. if (master == NULL) {
  369. dev_err(&pdev->dev, "No memory for spi_master\n");
  370. return -ENOMEM;
  371. }
  372. hw = spi_master_get_devdata(master);
  373. hw->master = master;
  374. hw->pdata = pdata = dev_get_platdata(&pdev->dev);
  375. hw->dev = &pdev->dev;
  376. if (pdata == NULL) {
  377. dev_err(&pdev->dev, "No platform data supplied\n");
  378. return -ENOENT;
  379. }
  380. platform_set_drvdata(pdev, hw);
  381. init_completion(&hw->done);
  382. /* initialise fiq handler */
  383. s3c24xx_spi_initfiq(hw);
  384. /* setup the master state. */
  385. /* the spi->mode bits understood by this driver: */
  386. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  387. master->num_chipselect = hw->pdata->num_cs;
  388. master->bus_num = pdata->bus_num;
  389. master->bits_per_word_mask = SPI_BPW_MASK(8);
  390. /* we need to call the local chipselect callback */
  391. master->flags = SPI_MASTER_GPIO_SS;
  392. master->use_gpio_descriptors = true;
  393. /* setup the state for the bitbang driver */
  394. hw->bitbang.master = hw->master;
  395. hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;
  396. hw->bitbang.chipselect = s3c24xx_spi_chipsel;
  397. hw->bitbang.txrx_bufs = s3c24xx_spi_txrx;
  398. hw->master->setup = s3c24xx_spi_setup;
  399. dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
  400. /* find and map our resources */
  401. hw->regs = devm_platform_ioremap_resource(pdev, 0);
  402. if (IS_ERR(hw->regs))
  403. return PTR_ERR(hw->regs);
  404. hw->irq = platform_get_irq(pdev, 0);
  405. if (hw->irq < 0)
  406. return -ENOENT;
  407. err = devm_request_irq(&pdev->dev, hw->irq, s3c24xx_spi_irq, 0,
  408. pdev->name, hw);
  409. if (err) {
  410. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  411. return err;
  412. }
  413. hw->clk = devm_clk_get(&pdev->dev, "spi");
  414. if (IS_ERR(hw->clk)) {
  415. dev_err(&pdev->dev, "No clock for device\n");
  416. return PTR_ERR(hw->clk);
  417. }
  418. s3c24xx_spi_initialsetup(hw);
  419. /* register our spi controller */
  420. err = spi_bitbang_start(&hw->bitbang);
  421. if (err) {
  422. dev_err(&pdev->dev, "Failed to register SPI master\n");
  423. goto err_register;
  424. }
  425. return 0;
  426. err_register:
  427. clk_disable(hw->clk);
  428. return err;
  429. }
  430. static int s3c24xx_spi_remove(struct platform_device *dev)
  431. {
  432. struct s3c24xx_spi *hw = platform_get_drvdata(dev);
  433. spi_bitbang_stop(&hw->bitbang);
  434. clk_disable(hw->clk);
  435. spi_master_put(hw->master);
  436. return 0;
  437. }
  438. #ifdef CONFIG_PM
  439. static int s3c24xx_spi_suspend(struct device *dev)
  440. {
  441. struct s3c24xx_spi *hw = dev_get_drvdata(dev);
  442. int ret;
  443. ret = spi_master_suspend(hw->master);
  444. if (ret)
  445. return ret;
  446. clk_disable(hw->clk);
  447. return 0;
  448. }
  449. static int s3c24xx_spi_resume(struct device *dev)
  450. {
  451. struct s3c24xx_spi *hw = dev_get_drvdata(dev);
  452. s3c24xx_spi_initialsetup(hw);
  453. return spi_master_resume(hw->master);
  454. }
  455. static const struct dev_pm_ops s3c24xx_spi_pmops = {
  456. .suspend = s3c24xx_spi_suspend,
  457. .resume = s3c24xx_spi_resume,
  458. };
  459. #define S3C24XX_SPI_PMOPS &s3c24xx_spi_pmops
  460. #else
  461. #define S3C24XX_SPI_PMOPS NULL
  462. #endif /* CONFIG_PM */
  463. MODULE_ALIAS("platform:s3c2410-spi");
  464. static struct platform_driver s3c24xx_spi_driver = {
  465. .probe = s3c24xx_spi_probe,
  466. .remove = s3c24xx_spi_remove,
  467. .driver = {
  468. .name = "s3c2410-spi",
  469. .pm = S3C24XX_SPI_PMOPS,
  470. },
  471. };
  472. module_platform_driver(s3c24xx_spi_driver);
  473. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  474. MODULE_AUTHOR("Ben Dooks, <[email protected]>");
  475. MODULE_LICENSE("GPL");