spi-efm32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (C) 2012-2013 Uwe Kleine-Koenig for Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU General Public License version 2 as published by the
  6. * Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/io.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/spi/spi_bitbang.h>
  12. #include <linux/gpio.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/platform_data/efm32-spi.h>
  20. #define DRIVER_NAME "efm32-spi"
  21. #define MASK_VAL(mask, val) ((val << __ffs(mask)) & mask)
  22. #define REG_CTRL 0x00
  23. #define REG_CTRL_SYNC 0x0001
  24. #define REG_CTRL_CLKPOL 0x0100
  25. #define REG_CTRL_CLKPHA 0x0200
  26. #define REG_CTRL_MSBF 0x0400
  27. #define REG_CTRL_TXBIL 0x1000
  28. #define REG_FRAME 0x04
  29. #define REG_FRAME_DATABITS__MASK 0x000f
  30. #define REG_FRAME_DATABITS(n) ((n) - 3)
  31. #define REG_CMD 0x0c
  32. #define REG_CMD_RXEN 0x0001
  33. #define REG_CMD_RXDIS 0x0002
  34. #define REG_CMD_TXEN 0x0004
  35. #define REG_CMD_TXDIS 0x0008
  36. #define REG_CMD_MASTEREN 0x0010
  37. #define REG_STATUS 0x10
  38. #define REG_STATUS_TXENS 0x0002
  39. #define REG_STATUS_TXC 0x0020
  40. #define REG_STATUS_TXBL 0x0040
  41. #define REG_STATUS_RXDATAV 0x0080
  42. #define REG_CLKDIV 0x14
  43. #define REG_RXDATAX 0x18
  44. #define REG_RXDATAX_RXDATA__MASK 0x01ff
  45. #define REG_RXDATAX_PERR 0x4000
  46. #define REG_RXDATAX_FERR 0x8000
  47. #define REG_TXDATA 0x34
  48. #define REG_IF 0x40
  49. #define REG_IF_TXBL 0x0002
  50. #define REG_IF_RXDATAV 0x0004
  51. #define REG_IFS 0x44
  52. #define REG_IFC 0x48
  53. #define REG_IEN 0x4c
  54. #define REG_ROUTE 0x54
  55. #define REG_ROUTE_RXPEN 0x0001
  56. #define REG_ROUTE_TXPEN 0x0002
  57. #define REG_ROUTE_CLKPEN 0x0008
  58. #define REG_ROUTE_LOCATION__MASK 0x0700
  59. #define REG_ROUTE_LOCATION(n) MASK_VAL(REG_ROUTE_LOCATION__MASK, (n))
  60. struct efm32_spi_ddata {
  61. struct spi_bitbang bitbang;
  62. spinlock_t lock;
  63. struct clk *clk;
  64. void __iomem *base;
  65. unsigned int rxirq, txirq;
  66. struct efm32_spi_pdata pdata;
  67. /* irq data */
  68. struct completion done;
  69. const u8 *tx_buf;
  70. u8 *rx_buf;
  71. unsigned tx_len, rx_len;
  72. /* chip selects */
  73. unsigned csgpio[];
  74. };
  75. #define ddata_to_dev(ddata) (&(ddata->bitbang.master->dev))
  76. #define efm32_spi_vdbg(ddata, format, arg...) \
  77. dev_vdbg(ddata_to_dev(ddata), format, ##arg)
  78. static void efm32_spi_write32(struct efm32_spi_ddata *ddata,
  79. u32 value, unsigned offset)
  80. {
  81. writel_relaxed(value, ddata->base + offset);
  82. }
  83. static u32 efm32_spi_read32(struct efm32_spi_ddata *ddata, unsigned offset)
  84. {
  85. return readl_relaxed(ddata->base + offset);
  86. }
  87. static void efm32_spi_chipselect(struct spi_device *spi, int is_on)
  88. {
  89. struct efm32_spi_ddata *ddata = spi_master_get_devdata(spi->master);
  90. int value = !(spi->mode & SPI_CS_HIGH) == !(is_on == BITBANG_CS_ACTIVE);
  91. gpio_set_value(ddata->csgpio[spi->chip_select], value);
  92. }
  93. static int efm32_spi_setup_transfer(struct spi_device *spi,
  94. struct spi_transfer *t)
  95. {
  96. struct efm32_spi_ddata *ddata = spi_master_get_devdata(spi->master);
  97. unsigned bpw = t->bits_per_word ?: spi->bits_per_word;
  98. unsigned speed = t->speed_hz ?: spi->max_speed_hz;
  99. unsigned long clkfreq = clk_get_rate(ddata->clk);
  100. u32 clkdiv;
  101. efm32_spi_write32(ddata, REG_CTRL_SYNC | REG_CTRL_MSBF |
  102. (spi->mode & SPI_CPHA ? REG_CTRL_CLKPHA : 0) |
  103. (spi->mode & SPI_CPOL ? REG_CTRL_CLKPOL : 0), REG_CTRL);
  104. efm32_spi_write32(ddata,
  105. REG_FRAME_DATABITS(bpw), REG_FRAME);
  106. if (2 * speed >= clkfreq)
  107. clkdiv = 0;
  108. else
  109. clkdiv = 64 * (DIV_ROUND_UP(2 * clkfreq, speed) - 4);
  110. if (clkdiv > (1U << 21))
  111. return -EINVAL;
  112. efm32_spi_write32(ddata, clkdiv, REG_CLKDIV);
  113. efm32_spi_write32(ddata, REG_CMD_MASTEREN, REG_CMD);
  114. efm32_spi_write32(ddata, REG_CMD_RXEN | REG_CMD_TXEN, REG_CMD);
  115. return 0;
  116. }
  117. static void efm32_spi_tx_u8(struct efm32_spi_ddata *ddata)
  118. {
  119. u8 val = 0;
  120. if (ddata->tx_buf) {
  121. val = *ddata->tx_buf;
  122. ddata->tx_buf++;
  123. }
  124. ddata->tx_len--;
  125. efm32_spi_write32(ddata, val, REG_TXDATA);
  126. efm32_spi_vdbg(ddata, "%s: tx 0x%x\n", __func__, val);
  127. }
  128. static void efm32_spi_rx_u8(struct efm32_spi_ddata *ddata)
  129. {
  130. u32 rxdata = efm32_spi_read32(ddata, REG_RXDATAX);
  131. efm32_spi_vdbg(ddata, "%s: rx 0x%x\n", __func__, rxdata);
  132. if (ddata->rx_buf) {
  133. *ddata->rx_buf = rxdata;
  134. ddata->rx_buf++;
  135. }
  136. ddata->rx_len--;
  137. }
  138. static void efm32_spi_filltx(struct efm32_spi_ddata *ddata)
  139. {
  140. while (ddata->tx_len &&
  141. ddata->tx_len + 2 > ddata->rx_len &&
  142. efm32_spi_read32(ddata, REG_STATUS) & REG_STATUS_TXBL) {
  143. efm32_spi_tx_u8(ddata);
  144. }
  145. }
  146. static int efm32_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  147. {
  148. struct efm32_spi_ddata *ddata = spi_master_get_devdata(spi->master);
  149. int ret = -EBUSY;
  150. spin_lock_irq(&ddata->lock);
  151. if (ddata->tx_buf || ddata->rx_buf)
  152. goto out_unlock;
  153. ddata->tx_buf = t->tx_buf;
  154. ddata->rx_buf = t->rx_buf;
  155. ddata->tx_len = ddata->rx_len =
  156. t->len * DIV_ROUND_UP(t->bits_per_word, 8);
  157. efm32_spi_filltx(ddata);
  158. reinit_completion(&ddata->done);
  159. efm32_spi_write32(ddata, REG_IF_TXBL | REG_IF_RXDATAV, REG_IEN);
  160. spin_unlock_irq(&ddata->lock);
  161. wait_for_completion(&ddata->done);
  162. spin_lock_irq(&ddata->lock);
  163. ret = t->len - max(ddata->tx_len, ddata->rx_len);
  164. efm32_spi_write32(ddata, 0, REG_IEN);
  165. ddata->tx_buf = ddata->rx_buf = NULL;
  166. out_unlock:
  167. spin_unlock_irq(&ddata->lock);
  168. return ret;
  169. }
  170. static irqreturn_t efm32_spi_rxirq(int irq, void *data)
  171. {
  172. struct efm32_spi_ddata *ddata = data;
  173. irqreturn_t ret = IRQ_NONE;
  174. spin_lock(&ddata->lock);
  175. while (ddata->rx_len > 0 &&
  176. efm32_spi_read32(ddata, REG_STATUS) &
  177. REG_STATUS_RXDATAV) {
  178. efm32_spi_rx_u8(ddata);
  179. ret = IRQ_HANDLED;
  180. }
  181. if (!ddata->rx_len) {
  182. u32 ien = efm32_spi_read32(ddata, REG_IEN);
  183. ien &= ~REG_IF_RXDATAV;
  184. efm32_spi_write32(ddata, ien, REG_IEN);
  185. complete(&ddata->done);
  186. }
  187. spin_unlock(&ddata->lock);
  188. return ret;
  189. }
  190. static irqreturn_t efm32_spi_txirq(int irq, void *data)
  191. {
  192. struct efm32_spi_ddata *ddata = data;
  193. efm32_spi_vdbg(ddata,
  194. "%s: txlen = %u, rxlen = %u, if=0x%08x, stat=0x%08x\n",
  195. __func__, ddata->tx_len, ddata->rx_len,
  196. efm32_spi_read32(ddata, REG_IF),
  197. efm32_spi_read32(ddata, REG_STATUS));
  198. spin_lock(&ddata->lock);
  199. efm32_spi_filltx(ddata);
  200. efm32_spi_vdbg(ddata, "%s: txlen = %u, rxlen = %u\n",
  201. __func__, ddata->tx_len, ddata->rx_len);
  202. if (!ddata->tx_len) {
  203. u32 ien = efm32_spi_read32(ddata, REG_IEN);
  204. ien &= ~REG_IF_TXBL;
  205. efm32_spi_write32(ddata, ien, REG_IEN);
  206. efm32_spi_vdbg(ddata, "disable TXBL\n");
  207. }
  208. spin_unlock(&ddata->lock);
  209. return IRQ_HANDLED;
  210. }
  211. static u32 efm32_spi_get_configured_location(struct efm32_spi_ddata *ddata)
  212. {
  213. u32 reg = efm32_spi_read32(ddata, REG_ROUTE);
  214. return (reg & REG_ROUTE_LOCATION__MASK) >> __ffs(REG_ROUTE_LOCATION__MASK);
  215. }
  216. static void efm32_spi_probe_dt(struct platform_device *pdev,
  217. struct spi_master *master, struct efm32_spi_ddata *ddata)
  218. {
  219. struct device_node *np = pdev->dev.of_node;
  220. u32 location;
  221. int ret;
  222. ret = of_property_read_u32(np, "energymicro,location", &location);
  223. if (ret)
  224. /* fall back to wrongly namespaced property */
  225. ret = of_property_read_u32(np, "efm32,location", &location);
  226. if (ret)
  227. /* fall back to old and (wrongly) generic property "location" */
  228. ret = of_property_read_u32(np, "location", &location);
  229. if (!ret) {
  230. dev_dbg(&pdev->dev, "using location %u\n", location);
  231. } else {
  232. /* default to location configured in hardware */
  233. location = efm32_spi_get_configured_location(ddata);
  234. dev_info(&pdev->dev, "fall back to location %u\n", location);
  235. }
  236. ddata->pdata.location = location;
  237. }
  238. static int efm32_spi_probe(struct platform_device *pdev)
  239. {
  240. struct efm32_spi_ddata *ddata;
  241. struct resource *res;
  242. int ret;
  243. struct spi_master *master;
  244. struct device_node *np = pdev->dev.of_node;
  245. int num_cs, i;
  246. if (!np)
  247. return -EINVAL;
  248. num_cs = of_gpio_named_count(np, "cs-gpios");
  249. if (num_cs < 0)
  250. return num_cs;
  251. master = spi_alloc_master(&pdev->dev,
  252. sizeof(*ddata) + num_cs * sizeof(unsigned));
  253. if (!master) {
  254. dev_dbg(&pdev->dev,
  255. "failed to allocate spi master controller\n");
  256. return -ENOMEM;
  257. }
  258. platform_set_drvdata(pdev, master);
  259. master->dev.of_node = pdev->dev.of_node;
  260. master->num_chipselect = num_cs;
  261. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  262. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  263. ddata = spi_master_get_devdata(master);
  264. ddata->bitbang.master = master;
  265. ddata->bitbang.chipselect = efm32_spi_chipselect;
  266. ddata->bitbang.setup_transfer = efm32_spi_setup_transfer;
  267. ddata->bitbang.txrx_bufs = efm32_spi_txrx_bufs;
  268. spin_lock_init(&ddata->lock);
  269. init_completion(&ddata->done);
  270. ddata->clk = devm_clk_get(&pdev->dev, NULL);
  271. if (IS_ERR(ddata->clk)) {
  272. ret = PTR_ERR(ddata->clk);
  273. dev_err(&pdev->dev, "failed to get clock: %d\n", ret);
  274. goto err;
  275. }
  276. for (i = 0; i < num_cs; ++i) {
  277. ret = of_get_named_gpio(np, "cs-gpios", i);
  278. if (ret < 0) {
  279. dev_err(&pdev->dev, "failed to get csgpio#%u (%d)\n",
  280. i, ret);
  281. goto err;
  282. }
  283. ddata->csgpio[i] = ret;
  284. dev_dbg(&pdev->dev, "csgpio#%u = %u\n", i, ddata->csgpio[i]);
  285. ret = devm_gpio_request_one(&pdev->dev, ddata->csgpio[i],
  286. GPIOF_OUT_INIT_LOW, DRIVER_NAME);
  287. if (ret < 0) {
  288. dev_err(&pdev->dev,
  289. "failed to configure csgpio#%u (%d)\n",
  290. i, ret);
  291. goto err;
  292. }
  293. }
  294. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. if (!res) {
  296. ret = -ENODEV;
  297. dev_err(&pdev->dev, "failed to determine base address\n");
  298. goto err;
  299. }
  300. if (resource_size(res) < 0x60) {
  301. ret = -EINVAL;
  302. dev_err(&pdev->dev, "memory resource too small\n");
  303. goto err;
  304. }
  305. ddata->base = devm_ioremap_resource(&pdev->dev, res);
  306. if (IS_ERR(ddata->base)) {
  307. ret = PTR_ERR(ddata->base);
  308. goto err;
  309. }
  310. ret = platform_get_irq(pdev, 0);
  311. if (ret <= 0) {
  312. dev_err(&pdev->dev, "failed to get rx irq (%d)\n", ret);
  313. goto err;
  314. }
  315. ddata->rxirq = ret;
  316. ret = platform_get_irq(pdev, 1);
  317. if (ret <= 0)
  318. ret = ddata->rxirq + 1;
  319. ddata->txirq = ret;
  320. ret = clk_prepare_enable(ddata->clk);
  321. if (ret < 0) {
  322. dev_err(&pdev->dev, "failed to enable clock (%d)\n", ret);
  323. goto err;
  324. }
  325. efm32_spi_probe_dt(pdev, master, ddata);
  326. efm32_spi_write32(ddata, 0, REG_IEN);
  327. efm32_spi_write32(ddata, REG_ROUTE_TXPEN | REG_ROUTE_RXPEN |
  328. REG_ROUTE_CLKPEN |
  329. REG_ROUTE_LOCATION(ddata->pdata.location), REG_ROUTE);
  330. ret = request_irq(ddata->rxirq, efm32_spi_rxirq,
  331. 0, DRIVER_NAME " rx", ddata);
  332. if (ret) {
  333. dev_err(&pdev->dev, "failed to register rxirq (%d)\n", ret);
  334. goto err_disable_clk;
  335. }
  336. ret = request_irq(ddata->txirq, efm32_spi_txirq,
  337. 0, DRIVER_NAME " tx", ddata);
  338. if (ret) {
  339. dev_err(&pdev->dev, "failed to register txirq (%d)\n", ret);
  340. goto err_free_rx_irq;
  341. }
  342. ret = spi_bitbang_start(&ddata->bitbang);
  343. if (ret) {
  344. dev_err(&pdev->dev, "spi_bitbang_start failed (%d)\n", ret);
  345. free_irq(ddata->txirq, ddata);
  346. err_free_rx_irq:
  347. free_irq(ddata->rxirq, ddata);
  348. err_disable_clk:
  349. clk_disable_unprepare(ddata->clk);
  350. err:
  351. spi_master_put(master);
  352. }
  353. return ret;
  354. }
  355. static int efm32_spi_remove(struct platform_device *pdev)
  356. {
  357. struct spi_master *master = platform_get_drvdata(pdev);
  358. struct efm32_spi_ddata *ddata = spi_master_get_devdata(master);
  359. spi_bitbang_stop(&ddata->bitbang);
  360. efm32_spi_write32(ddata, 0, REG_IEN);
  361. free_irq(ddata->txirq, ddata);
  362. free_irq(ddata->rxirq, ddata);
  363. clk_disable_unprepare(ddata->clk);
  364. spi_master_put(master);
  365. return 0;
  366. }
  367. static const struct of_device_id efm32_spi_dt_ids[] = {
  368. {
  369. .compatible = "energymicro,efm32-spi",
  370. }, {
  371. /* doesn't follow the "vendor,device" scheme, don't use */
  372. .compatible = "efm32,spi",
  373. }, {
  374. /* sentinel */
  375. }
  376. };
  377. MODULE_DEVICE_TABLE(of, efm32_spi_dt_ids);
  378. static struct platform_driver efm32_spi_driver = {
  379. .probe = efm32_spi_probe,
  380. .remove = efm32_spi_remove,
  381. .driver = {
  382. .name = DRIVER_NAME,
  383. .of_match_table = efm32_spi_dt_ids,
  384. },
  385. };
  386. module_platform_driver(efm32_spi_driver);
  387. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  388. MODULE_DESCRIPTION("EFM32 SPI driver");
  389. MODULE_LICENSE("GPL v2");
  390. MODULE_ALIAS("platform:" DRIVER_NAME);