spi-omap-100k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * OMAP7xx SPI 100k controller driver
  3. * Author: Fabrice Crohas <fcrohas@gmail.com>
  4. * from original omap1_mcspi driver
  5. *
  6. * Copyright (C) 2005, 2006 Nokia Corporation
  7. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  8. * Juha Yrj�l� <juha.yrjola@nokia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/delay.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/err.h>
  29. #include <linux/clk.h>
  30. #include <linux/io.h>
  31. #include <linux/gpio.h>
  32. #include <linux/slab.h>
  33. #include <linux/spi/spi.h>
  34. #define OMAP1_SPI100K_MAX_FREQ 48000000
  35. #define ICR_SPITAS (OMAP7XX_ICR_BASE + 0x12)
  36. #define SPI_SETUP1 0x00
  37. #define SPI_SETUP2 0x02
  38. #define SPI_CTRL 0x04
  39. #define SPI_STATUS 0x06
  40. #define SPI_TX_LSB 0x08
  41. #define SPI_TX_MSB 0x0a
  42. #define SPI_RX_LSB 0x0c
  43. #define SPI_RX_MSB 0x0e
  44. #define SPI_SETUP1_INT_READ_ENABLE (1UL << 5)
  45. #define SPI_SETUP1_INT_WRITE_ENABLE (1UL << 4)
  46. #define SPI_SETUP1_CLOCK_DIVISOR(x) ((x) << 1)
  47. #define SPI_SETUP1_CLOCK_ENABLE (1UL << 0)
  48. #define SPI_SETUP2_ACTIVE_EDGE_FALLING (0UL << 0)
  49. #define SPI_SETUP2_ACTIVE_EDGE_RISING (1UL << 0)
  50. #define SPI_SETUP2_NEGATIVE_LEVEL (0UL << 5)
  51. #define SPI_SETUP2_POSITIVE_LEVEL (1UL << 5)
  52. #define SPI_SETUP2_LEVEL_TRIGGER (0UL << 10)
  53. #define SPI_SETUP2_EDGE_TRIGGER (1UL << 10)
  54. #define SPI_CTRL_SEN(x) ((x) << 7)
  55. #define SPI_CTRL_WORD_SIZE(x) (((x) - 1) << 2)
  56. #define SPI_CTRL_WR (1UL << 1)
  57. #define SPI_CTRL_RD (1UL << 0)
  58. #define SPI_STATUS_WE (1UL << 1)
  59. #define SPI_STATUS_RD (1UL << 0)
  60. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  61. * cache operations; better heuristics consider wordsize and bitrate.
  62. */
  63. #define DMA_MIN_BYTES 8
  64. #define SPI_RUNNING 0
  65. #define SPI_SHUTDOWN 1
  66. struct omap1_spi100k {
  67. struct clk *ick;
  68. struct clk *fck;
  69. /* Virtual base address of the controller */
  70. void __iomem *base;
  71. };
  72. struct omap1_spi100k_cs {
  73. void __iomem *base;
  74. int word_len;
  75. };
  76. static void spi100k_enable_clock(struct spi_master *master)
  77. {
  78. unsigned int val;
  79. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  80. /* enable SPI */
  81. val = readw(spi100k->base + SPI_SETUP1);
  82. val |= SPI_SETUP1_CLOCK_ENABLE;
  83. writew(val, spi100k->base + SPI_SETUP1);
  84. }
  85. static void spi100k_disable_clock(struct spi_master *master)
  86. {
  87. unsigned int val;
  88. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  89. /* disable SPI */
  90. val = readw(spi100k->base + SPI_SETUP1);
  91. val &= ~SPI_SETUP1_CLOCK_ENABLE;
  92. writew(val, spi100k->base + SPI_SETUP1);
  93. }
  94. static void spi100k_write_data(struct spi_master *master, int len, int data)
  95. {
  96. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  97. /* write 16-bit word, shifting 8-bit data if necessary */
  98. if (len <= 8) {
  99. data <<= 8;
  100. len = 16;
  101. }
  102. spi100k_enable_clock(master);
  103. writew(data , spi100k->base + SPI_TX_MSB);
  104. writew(SPI_CTRL_SEN(0) |
  105. SPI_CTRL_WORD_SIZE(len) |
  106. SPI_CTRL_WR,
  107. spi100k->base + SPI_CTRL);
  108. /* Wait for bit ack send change */
  109. while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_WE) != SPI_STATUS_WE)
  110. ;
  111. udelay(1000);
  112. spi100k_disable_clock(master);
  113. }
  114. static int spi100k_read_data(struct spi_master *master, int len)
  115. {
  116. int dataH, dataL;
  117. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  118. /* Always do at least 16 bits */
  119. if (len <= 8)
  120. len = 16;
  121. spi100k_enable_clock(master);
  122. writew(SPI_CTRL_SEN(0) |
  123. SPI_CTRL_WORD_SIZE(len) |
  124. SPI_CTRL_RD,
  125. spi100k->base + SPI_CTRL);
  126. while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_RD) != SPI_STATUS_RD)
  127. ;
  128. udelay(1000);
  129. dataL = readw(spi100k->base + SPI_RX_LSB);
  130. dataH = readw(spi100k->base + SPI_RX_MSB);
  131. spi100k_disable_clock(master);
  132. return dataL;
  133. }
  134. static void spi100k_open(struct spi_master *master)
  135. {
  136. /* get control of SPI */
  137. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  138. writew(SPI_SETUP1_INT_READ_ENABLE |
  139. SPI_SETUP1_INT_WRITE_ENABLE |
  140. SPI_SETUP1_CLOCK_DIVISOR(0), spi100k->base + SPI_SETUP1);
  141. /* configure clock and interrupts */
  142. writew(SPI_SETUP2_ACTIVE_EDGE_FALLING |
  143. SPI_SETUP2_NEGATIVE_LEVEL |
  144. SPI_SETUP2_LEVEL_TRIGGER, spi100k->base + SPI_SETUP2);
  145. }
  146. static void omap1_spi100k_force_cs(struct omap1_spi100k *spi100k, int enable)
  147. {
  148. if (enable)
  149. writew(0x05fc, spi100k->base + SPI_CTRL);
  150. else
  151. writew(0x05fd, spi100k->base + SPI_CTRL);
  152. }
  153. static unsigned
  154. omap1_spi100k_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  155. {
  156. struct omap1_spi100k_cs *cs = spi->controller_state;
  157. unsigned int count, c;
  158. int word_len;
  159. count = xfer->len;
  160. c = count;
  161. word_len = cs->word_len;
  162. if (word_len <= 8) {
  163. u8 *rx;
  164. const u8 *tx;
  165. rx = xfer->rx_buf;
  166. tx = xfer->tx_buf;
  167. do {
  168. c -= 1;
  169. if (xfer->tx_buf != NULL)
  170. spi100k_write_data(spi->master, word_len, *tx++);
  171. if (xfer->rx_buf != NULL)
  172. *rx++ = spi100k_read_data(spi->master, word_len);
  173. } while (c);
  174. } else if (word_len <= 16) {
  175. u16 *rx;
  176. const u16 *tx;
  177. rx = xfer->rx_buf;
  178. tx = xfer->tx_buf;
  179. do {
  180. c -= 2;
  181. if (xfer->tx_buf != NULL)
  182. spi100k_write_data(spi->master, word_len, *tx++);
  183. if (xfer->rx_buf != NULL)
  184. *rx++ = spi100k_read_data(spi->master, word_len);
  185. } while (c);
  186. } else if (word_len <= 32) {
  187. u32 *rx;
  188. const u32 *tx;
  189. rx = xfer->rx_buf;
  190. tx = xfer->tx_buf;
  191. do {
  192. c -= 4;
  193. if (xfer->tx_buf != NULL)
  194. spi100k_write_data(spi->master, word_len, *tx);
  195. if (xfer->rx_buf != NULL)
  196. *rx = spi100k_read_data(spi->master, word_len);
  197. } while (c);
  198. }
  199. return count - c;
  200. }
  201. /* called only when no transfer is active to this device */
  202. static int omap1_spi100k_setup_transfer(struct spi_device *spi,
  203. struct spi_transfer *t)
  204. {
  205. struct omap1_spi100k *spi100k = spi_master_get_devdata(spi->master);
  206. struct omap1_spi100k_cs *cs = spi->controller_state;
  207. u8 word_len;
  208. if (t != NULL)
  209. word_len = t->bits_per_word;
  210. else
  211. word_len = spi->bits_per_word;
  212. if (spi->bits_per_word > 32)
  213. return -EINVAL;
  214. cs->word_len = word_len;
  215. /* SPI init before transfer */
  216. writew(0x3e , spi100k->base + SPI_SETUP1);
  217. writew(0x00 , spi100k->base + SPI_STATUS);
  218. writew(0x3e , spi100k->base + SPI_CTRL);
  219. return 0;
  220. }
  221. /* the spi->mode bits understood by this driver: */
  222. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  223. static int omap1_spi100k_setup(struct spi_device *spi)
  224. {
  225. int ret;
  226. struct omap1_spi100k *spi100k;
  227. struct omap1_spi100k_cs *cs = spi->controller_state;
  228. spi100k = spi_master_get_devdata(spi->master);
  229. if (!cs) {
  230. cs = devm_kzalloc(&spi->dev, sizeof(*cs), GFP_KERNEL);
  231. if (!cs)
  232. return -ENOMEM;
  233. cs->base = spi100k->base + spi->chip_select * 0x14;
  234. spi->controller_state = cs;
  235. }
  236. spi100k_open(spi->master);
  237. clk_prepare_enable(spi100k->ick);
  238. clk_prepare_enable(spi100k->fck);
  239. ret = omap1_spi100k_setup_transfer(spi, NULL);
  240. clk_disable_unprepare(spi100k->ick);
  241. clk_disable_unprepare(spi100k->fck);
  242. return ret;
  243. }
  244. static int omap1_spi100k_transfer_one_message(struct spi_master *master,
  245. struct spi_message *m)
  246. {
  247. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  248. struct spi_device *spi = m->spi;
  249. struct spi_transfer *t = NULL;
  250. int cs_active = 0;
  251. int status = 0;
  252. list_for_each_entry(t, &m->transfers, transfer_list) {
  253. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  254. status = -EINVAL;
  255. break;
  256. }
  257. status = omap1_spi100k_setup_transfer(spi, t);
  258. if (status < 0)
  259. break;
  260. if (!cs_active) {
  261. omap1_spi100k_force_cs(spi100k, 1);
  262. cs_active = 1;
  263. }
  264. if (t->len) {
  265. unsigned count;
  266. count = omap1_spi100k_txrx_pio(spi, t);
  267. m->actual_length += count;
  268. if (count != t->len) {
  269. status = -EIO;
  270. break;
  271. }
  272. }
  273. if (t->delay_usecs)
  274. udelay(t->delay_usecs);
  275. /* ignore the "leave it on after last xfer" hint */
  276. if (t->cs_change) {
  277. omap1_spi100k_force_cs(spi100k, 0);
  278. cs_active = 0;
  279. }
  280. }
  281. status = omap1_spi100k_setup_transfer(spi, NULL);
  282. if (cs_active)
  283. omap1_spi100k_force_cs(spi100k, 0);
  284. m->status = status;
  285. spi_finalize_current_message(master);
  286. return status;
  287. }
  288. static int omap1_spi100k_probe(struct platform_device *pdev)
  289. {
  290. struct spi_master *master;
  291. struct omap1_spi100k *spi100k;
  292. int status = 0;
  293. if (!pdev->id)
  294. return -EINVAL;
  295. master = spi_alloc_master(&pdev->dev, sizeof(*spi100k));
  296. if (master == NULL) {
  297. dev_dbg(&pdev->dev, "master allocation failed\n");
  298. return -ENOMEM;
  299. }
  300. if (pdev->id != -1)
  301. master->bus_num = pdev->id;
  302. master->setup = omap1_spi100k_setup;
  303. master->transfer_one_message = omap1_spi100k_transfer_one_message;
  304. master->num_chipselect = 2;
  305. master->mode_bits = MODEBITS;
  306. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  307. master->min_speed_hz = OMAP1_SPI100K_MAX_FREQ/(1<<16);
  308. master->max_speed_hz = OMAP1_SPI100K_MAX_FREQ;
  309. master->auto_runtime_pm = true;
  310. spi100k = spi_master_get_devdata(master);
  311. /*
  312. * The memory region base address is taken as the platform_data.
  313. * You should allocate this with ioremap() before initializing
  314. * the SPI.
  315. */
  316. spi100k->base = (void __iomem *)dev_get_platdata(&pdev->dev);
  317. spi100k->ick = devm_clk_get(&pdev->dev, "ick");
  318. if (IS_ERR(spi100k->ick)) {
  319. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  320. status = PTR_ERR(spi100k->ick);
  321. goto err;
  322. }
  323. spi100k->fck = devm_clk_get(&pdev->dev, "fck");
  324. if (IS_ERR(spi100k->fck)) {
  325. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  326. status = PTR_ERR(spi100k->fck);
  327. goto err;
  328. }
  329. status = clk_prepare_enable(spi100k->ick);
  330. if (status != 0) {
  331. dev_err(&pdev->dev, "failed to enable ick: %d\n", status);
  332. goto err;
  333. }
  334. status = clk_prepare_enable(spi100k->fck);
  335. if (status != 0) {
  336. dev_err(&pdev->dev, "failed to enable fck: %d\n", status);
  337. goto err_ick;
  338. }
  339. pm_runtime_enable(&pdev->dev);
  340. pm_runtime_set_active(&pdev->dev);
  341. status = devm_spi_register_master(&pdev->dev, master);
  342. if (status < 0)
  343. goto err_fck;
  344. return status;
  345. err_fck:
  346. clk_disable_unprepare(spi100k->fck);
  347. err_ick:
  348. clk_disable_unprepare(spi100k->ick);
  349. err:
  350. spi_master_put(master);
  351. return status;
  352. }
  353. static int omap1_spi100k_remove(struct platform_device *pdev)
  354. {
  355. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  356. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  357. pm_runtime_disable(&pdev->dev);
  358. clk_disable_unprepare(spi100k->fck);
  359. clk_disable_unprepare(spi100k->ick);
  360. return 0;
  361. }
  362. #ifdef CONFIG_PM
  363. static int omap1_spi100k_runtime_suspend(struct device *dev)
  364. {
  365. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  366. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  367. clk_disable_unprepare(spi100k->ick);
  368. clk_disable_unprepare(spi100k->fck);
  369. return 0;
  370. }
  371. static int omap1_spi100k_runtime_resume(struct device *dev)
  372. {
  373. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  374. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  375. int ret;
  376. ret = clk_prepare_enable(spi100k->ick);
  377. if (ret != 0) {
  378. dev_err(dev, "Failed to enable ick: %d\n", ret);
  379. return ret;
  380. }
  381. ret = clk_prepare_enable(spi100k->fck);
  382. if (ret != 0) {
  383. dev_err(dev, "Failed to enable fck: %d\n", ret);
  384. clk_disable_unprepare(spi100k->ick);
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. #endif
  390. static const struct dev_pm_ops omap1_spi100k_pm = {
  391. SET_RUNTIME_PM_OPS(omap1_spi100k_runtime_suspend,
  392. omap1_spi100k_runtime_resume, NULL)
  393. };
  394. static struct platform_driver omap1_spi100k_driver = {
  395. .driver = {
  396. .name = "omap1_spi100k",
  397. .pm = &omap1_spi100k_pm,
  398. },
  399. .probe = omap1_spi100k_probe,
  400. .remove = omap1_spi100k_remove,
  401. };
  402. module_platform_driver(omap1_spi100k_driver);
  403. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  404. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  405. MODULE_LICENSE("GPL");