spi-sh-hspi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * SuperH HSPI bus driver
  3. *
  4. * Copyright (C) 2011 Kuninori Morimoto
  5. *
  6. * Based on spi-sh.c:
  7. * Based on pxa2xx_spi.c:
  8. * Copyright (C) 2011 Renesas Solutions Corp.
  9. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  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. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. #include <linux/clk.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/timer.h>
  29. #include <linux/delay.h>
  30. #include <linux/list.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/io.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/spi/sh_hspi.h>
  37. #define SPCR 0x00
  38. #define SPSR 0x04
  39. #define SPSCR 0x08
  40. #define SPTBR 0x0C
  41. #define SPRBR 0x10
  42. #define SPCR2 0x14
  43. /* SPSR */
  44. #define RXFL (1 << 2)
  45. #define hspi2info(h) (h->dev->platform_data)
  46. struct hspi_priv {
  47. void __iomem *addr;
  48. struct spi_master *master;
  49. struct device *dev;
  50. struct clk *clk;
  51. };
  52. /*
  53. * basic function
  54. */
  55. static void hspi_write(struct hspi_priv *hspi, int reg, u32 val)
  56. {
  57. iowrite32(val, hspi->addr + reg);
  58. }
  59. static u32 hspi_read(struct hspi_priv *hspi, int reg)
  60. {
  61. return ioread32(hspi->addr + reg);
  62. }
  63. static void hspi_bit_set(struct hspi_priv *hspi, int reg, u32 mask, u32 set)
  64. {
  65. u32 val = hspi_read(hspi, reg);
  66. val &= ~mask;
  67. val |= set & mask;
  68. hspi_write(hspi, reg, val);
  69. }
  70. /*
  71. * transfer function
  72. */
  73. static int hspi_status_check_timeout(struct hspi_priv *hspi, u32 mask, u32 val)
  74. {
  75. int t = 256;
  76. while (t--) {
  77. if ((mask & hspi_read(hspi, SPSR)) == val)
  78. return 0;
  79. udelay(10);
  80. }
  81. dev_err(hspi->dev, "timeout\n");
  82. return -ETIMEDOUT;
  83. }
  84. /*
  85. * spi master function
  86. */
  87. #define hspi_hw_cs_enable(hspi) hspi_hw_cs_ctrl(hspi, 0)
  88. #define hspi_hw_cs_disable(hspi) hspi_hw_cs_ctrl(hspi, 1)
  89. static void hspi_hw_cs_ctrl(struct hspi_priv *hspi, int hi)
  90. {
  91. hspi_bit_set(hspi, SPSCR, (1 << 6), (hi) << 6);
  92. }
  93. static void hspi_hw_setup(struct hspi_priv *hspi,
  94. struct spi_message *msg,
  95. struct spi_transfer *t)
  96. {
  97. struct spi_device *spi = msg->spi;
  98. struct device *dev = hspi->dev;
  99. u32 target_rate;
  100. u32 spcr, idiv_clk;
  101. u32 rate, best_rate, min, tmp;
  102. target_rate = t ? t->speed_hz : 0;
  103. if (!target_rate)
  104. target_rate = spi->max_speed_hz;
  105. /*
  106. * find best IDIV/CLKCx settings
  107. */
  108. min = ~0;
  109. best_rate = 0;
  110. spcr = 0;
  111. for (idiv_clk = 0x00; idiv_clk <= 0x3F; idiv_clk++) {
  112. rate = clk_get_rate(hspi->clk);
  113. /* IDIV calculation */
  114. if (idiv_clk & (1 << 5))
  115. rate /= 128;
  116. else
  117. rate /= 16;
  118. /* CLKCx calculation */
  119. rate /= (((idiv_clk & 0x1F) + 1) * 2) ;
  120. /* save best settings */
  121. tmp = abs(target_rate - rate);
  122. if (tmp < min) {
  123. min = tmp;
  124. spcr = idiv_clk;
  125. best_rate = rate;
  126. }
  127. }
  128. if (spi->mode & SPI_CPHA)
  129. spcr |= 1 << 7;
  130. if (spi->mode & SPI_CPOL)
  131. spcr |= 1 << 6;
  132. dev_dbg(dev, "speed %d/%d\n", target_rate, best_rate);
  133. hspi_write(hspi, SPCR, spcr);
  134. hspi_write(hspi, SPSR, 0x0);
  135. hspi_write(hspi, SPSCR, 0x21); /* master mode / CS control */
  136. }
  137. static int hspi_transfer_one_message(struct spi_master *master,
  138. struct spi_message *msg)
  139. {
  140. struct hspi_priv *hspi = spi_master_get_devdata(master);
  141. struct spi_transfer *t;
  142. u32 tx;
  143. u32 rx;
  144. int ret, i;
  145. unsigned int cs_change;
  146. const int nsecs = 50;
  147. dev_dbg(hspi->dev, "%s\n", __func__);
  148. cs_change = 1;
  149. ret = 0;
  150. list_for_each_entry(t, &msg->transfers, transfer_list) {
  151. if (cs_change) {
  152. hspi_hw_setup(hspi, msg, t);
  153. hspi_hw_cs_enable(hspi);
  154. ndelay(nsecs);
  155. }
  156. cs_change = t->cs_change;
  157. for (i = 0; i < t->len; i++) {
  158. /* wait remains */
  159. ret = hspi_status_check_timeout(hspi, 0x1, 0);
  160. if (ret < 0)
  161. break;
  162. tx = 0;
  163. if (t->tx_buf)
  164. tx = (u32)((u8 *)t->tx_buf)[i];
  165. hspi_write(hspi, SPTBR, tx);
  166. /* wait recive */
  167. ret = hspi_status_check_timeout(hspi, 0x4, 0x4);
  168. if (ret < 0)
  169. break;
  170. rx = hspi_read(hspi, SPRBR);
  171. if (t->rx_buf)
  172. ((u8 *)t->rx_buf)[i] = (u8)rx;
  173. }
  174. msg->actual_length += t->len;
  175. if (t->delay_usecs)
  176. udelay(t->delay_usecs);
  177. if (cs_change) {
  178. ndelay(nsecs);
  179. hspi_hw_cs_disable(hspi);
  180. ndelay(nsecs);
  181. }
  182. }
  183. msg->status = ret;
  184. if (!cs_change) {
  185. ndelay(nsecs);
  186. hspi_hw_cs_disable(hspi);
  187. }
  188. spi_finalize_current_message(master);
  189. return ret;
  190. }
  191. static int hspi_setup(struct spi_device *spi)
  192. {
  193. struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
  194. struct device *dev = hspi->dev;
  195. if (8 != spi->bits_per_word) {
  196. dev_err(dev, "bits_per_word should be 8\n");
  197. return -EIO;
  198. }
  199. dev_dbg(dev, "%s setup\n", spi->modalias);
  200. return 0;
  201. }
  202. static void hspi_cleanup(struct spi_device *spi)
  203. {
  204. struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
  205. struct device *dev = hspi->dev;
  206. dev_dbg(dev, "%s cleanup\n", spi->modalias);
  207. }
  208. static int hspi_probe(struct platform_device *pdev)
  209. {
  210. struct resource *res;
  211. struct spi_master *master;
  212. struct hspi_priv *hspi;
  213. struct clk *clk;
  214. int ret;
  215. /* get base addr */
  216. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  217. if (!res) {
  218. dev_err(&pdev->dev, "invalid resource\n");
  219. return -EINVAL;
  220. }
  221. master = spi_alloc_master(&pdev->dev, sizeof(*hspi));
  222. if (!master) {
  223. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  224. return -ENOMEM;
  225. }
  226. clk = clk_get(NULL, "shyway_clk");
  227. if (IS_ERR(clk)) {
  228. dev_err(&pdev->dev, "shyway_clk is required\n");
  229. ret = -EINVAL;
  230. goto error0;
  231. }
  232. hspi = spi_master_get_devdata(master);
  233. platform_set_drvdata(pdev, hspi);
  234. /* init hspi */
  235. hspi->master = master;
  236. hspi->dev = &pdev->dev;
  237. hspi->clk = clk;
  238. hspi->addr = devm_ioremap(hspi->dev,
  239. res->start, resource_size(res));
  240. if (!hspi->addr) {
  241. dev_err(&pdev->dev, "ioremap error.\n");
  242. ret = -ENOMEM;
  243. goto error1;
  244. }
  245. master->num_chipselect = 1;
  246. master->bus_num = pdev->id;
  247. master->setup = hspi_setup;
  248. master->cleanup = hspi_cleanup;
  249. master->mode_bits = SPI_CPOL | SPI_CPHA;
  250. master->auto_runtime_pm = true;
  251. master->transfer_one_message = hspi_transfer_one_message;
  252. ret = devm_spi_register_master(&pdev->dev, master);
  253. if (ret < 0) {
  254. dev_err(&pdev->dev, "spi_register_master error.\n");
  255. goto error1;
  256. }
  257. pm_runtime_enable(&pdev->dev);
  258. return 0;
  259. error1:
  260. clk_put(clk);
  261. error0:
  262. spi_master_put(master);
  263. return ret;
  264. }
  265. static int hspi_remove(struct platform_device *pdev)
  266. {
  267. struct hspi_priv *hspi = platform_get_drvdata(pdev);
  268. pm_runtime_disable(&pdev->dev);
  269. clk_put(hspi->clk);
  270. return 0;
  271. }
  272. static struct platform_driver hspi_driver = {
  273. .probe = hspi_probe,
  274. .remove = hspi_remove,
  275. .driver = {
  276. .name = "sh-hspi",
  277. .owner = THIS_MODULE,
  278. },
  279. };
  280. module_platform_driver(hspi_driver);
  281. MODULE_DESCRIPTION("SuperH HSPI bus driver");
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  284. MODULE_ALIAS("platform:sh_spi");