spi-omap-100k.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 = spi->bits_per_word;
  208. if (t != NULL && t->bits_per_word)
  209. word_len = t->bits_per_word;
  210. if (!word_len)
  211. word_len = 8;
  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 par_override = 0;
  252. int status = 0;
  253. list_for_each_entry(t, &m->transfers, transfer_list) {
  254. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  255. status = -EINVAL;
  256. break;
  257. }
  258. if (par_override || t->speed_hz || t->bits_per_word) {
  259. par_override = 1;
  260. status = omap1_spi100k_setup_transfer(spi, t);
  261. if (status < 0)
  262. break;
  263. if (!t->speed_hz && !t->bits_per_word)
  264. par_override = 0;
  265. }
  266. if (!cs_active) {
  267. omap1_spi100k_force_cs(spi100k, 1);
  268. cs_active = 1;
  269. }
  270. if (t->len) {
  271. unsigned count;
  272. count = omap1_spi100k_txrx_pio(spi, t);
  273. m->actual_length += count;
  274. if (count != t->len) {
  275. status = -EIO;
  276. break;
  277. }
  278. }
  279. if (t->delay_usecs)
  280. udelay(t->delay_usecs);
  281. /* ignore the "leave it on after last xfer" hint */
  282. if (t->cs_change) {
  283. omap1_spi100k_force_cs(spi100k, 0);
  284. cs_active = 0;
  285. }
  286. }
  287. /* Restore defaults if they were overriden */
  288. if (par_override) {
  289. par_override = 0;
  290. status = omap1_spi100k_setup_transfer(spi, NULL);
  291. }
  292. if (cs_active)
  293. omap1_spi100k_force_cs(spi100k, 0);
  294. m->status = status;
  295. spi_finalize_current_message(master);
  296. return status;
  297. }
  298. static int omap1_spi100k_probe(struct platform_device *pdev)
  299. {
  300. struct spi_master *master;
  301. struct omap1_spi100k *spi100k;
  302. int status = 0;
  303. if (!pdev->id)
  304. return -EINVAL;
  305. master = spi_alloc_master(&pdev->dev, sizeof(*spi100k));
  306. if (master == NULL) {
  307. dev_dbg(&pdev->dev, "master allocation failed\n");
  308. return -ENOMEM;
  309. }
  310. if (pdev->id != -1)
  311. master->bus_num = pdev->id;
  312. master->setup = omap1_spi100k_setup;
  313. master->transfer_one_message = omap1_spi100k_transfer_one_message;
  314. master->num_chipselect = 2;
  315. master->mode_bits = MODEBITS;
  316. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  317. master->min_speed_hz = OMAP1_SPI100K_MAX_FREQ/(1<<16);
  318. master->max_speed_hz = OMAP1_SPI100K_MAX_FREQ;
  319. master->auto_runtime_pm = true;
  320. spi100k = spi_master_get_devdata(master);
  321. /*
  322. * The memory region base address is taken as the platform_data.
  323. * You should allocate this with ioremap() before initializing
  324. * the SPI.
  325. */
  326. spi100k->base = (void __iomem *)dev_get_platdata(&pdev->dev);
  327. spi100k->ick = devm_clk_get(&pdev->dev, "ick");
  328. if (IS_ERR(spi100k->ick)) {
  329. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  330. status = PTR_ERR(spi100k->ick);
  331. goto err;
  332. }
  333. spi100k->fck = devm_clk_get(&pdev->dev, "fck");
  334. if (IS_ERR(spi100k->fck)) {
  335. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  336. status = PTR_ERR(spi100k->fck);
  337. goto err;
  338. }
  339. status = clk_prepare_enable(spi100k->ick);
  340. if (status != 0) {
  341. dev_err(&pdev->dev, "failed to enable ick: %d\n", status);
  342. goto err;
  343. }
  344. status = clk_prepare_enable(spi100k->fck);
  345. if (status != 0) {
  346. dev_err(&pdev->dev, "failed to enable fck: %d\n", status);
  347. goto err_ick;
  348. }
  349. pm_runtime_enable(&pdev->dev);
  350. pm_runtime_set_active(&pdev->dev);
  351. status = devm_spi_register_master(&pdev->dev, master);
  352. if (status < 0)
  353. goto err_fck;
  354. return status;
  355. err_fck:
  356. clk_disable_unprepare(spi100k->fck);
  357. err_ick:
  358. clk_disable_unprepare(spi100k->ick);
  359. err:
  360. spi_master_put(master);
  361. return status;
  362. }
  363. static int omap1_spi100k_remove(struct platform_device *pdev)
  364. {
  365. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  366. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  367. pm_runtime_disable(&pdev->dev);
  368. clk_disable_unprepare(spi100k->fck);
  369. clk_disable_unprepare(spi100k->ick);
  370. return 0;
  371. }
  372. #ifdef CONFIG_PM
  373. static int omap1_spi100k_runtime_suspend(struct device *dev)
  374. {
  375. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  376. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  377. clk_disable_unprepare(spi100k->ick);
  378. clk_disable_unprepare(spi100k->fck);
  379. return 0;
  380. }
  381. static int omap1_spi100k_runtime_resume(struct device *dev)
  382. {
  383. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  384. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  385. int ret;
  386. ret = clk_prepare_enable(spi100k->ick);
  387. if (ret != 0) {
  388. dev_err(dev, "Failed to enable ick: %d\n", ret);
  389. return ret;
  390. }
  391. ret = clk_prepare_enable(spi100k->fck);
  392. if (ret != 0) {
  393. dev_err(dev, "Failed to enable fck: %d\n", ret);
  394. clk_disable_unprepare(spi100k->ick);
  395. return ret;
  396. }
  397. return 0;
  398. }
  399. #endif
  400. static const struct dev_pm_ops omap1_spi100k_pm = {
  401. SET_RUNTIME_PM_OPS(omap1_spi100k_runtime_suspend,
  402. omap1_spi100k_runtime_resume, NULL)
  403. };
  404. static struct platform_driver omap1_spi100k_driver = {
  405. .driver = {
  406. .name = "omap1_spi100k",
  407. .pm = &omap1_spi100k_pm,
  408. },
  409. .probe = omap1_spi100k_probe,
  410. .remove = omap1_spi100k_remove,
  411. };
  412. module_platform_driver(omap1_spi100k_driver);
  413. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  414. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  415. MODULE_LICENSE("GPL");