spi-omap2-mcspi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrj�l� <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/err.h>
  28. #include <linux/clk.h>
  29. #include <linux/io.h>
  30. #include <linux/slab.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/gcd.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/gpio.h>
  37. #include <linux/platform_data/spi-omap2-mcspi.h>
  38. #define OMAP2_MCSPI_MAX_FREQ 48000000
  39. #define OMAP2_MCSPI_MAX_DIVIDER 4096
  40. #define OMAP2_MCSPI_MAX_FIFODEPTH 64
  41. #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
  42. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  43. #define OMAP2_MCSPI_REVISION 0x00
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. #define OMAP2_MCSPI_XFERLEVEL 0x7c
  51. /* per-channel banks, 0x14 bytes each, first is: */
  52. #define OMAP2_MCSPI_CHCONF0 0x2c
  53. #define OMAP2_MCSPI_CHSTAT0 0x30
  54. #define OMAP2_MCSPI_CHCTRL0 0x34
  55. #define OMAP2_MCSPI_TX0 0x38
  56. #define OMAP2_MCSPI_RX0 0x3c
  57. /* per-register bitmasks: */
  58. #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
  59. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  60. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  61. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  62. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  63. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  64. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  65. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  66. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  67. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  68. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  69. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  70. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  71. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  72. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  73. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  74. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  75. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  76. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  77. #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
  78. #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
  79. #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
  80. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  81. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  82. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  83. #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
  84. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  85. #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
  86. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  87. /* We have 2 DMA channels per CS, one for RX and one for TX */
  88. struct omap2_mcspi_dma {
  89. struct dma_chan *dma_tx;
  90. struct dma_chan *dma_rx;
  91. struct completion dma_tx_completion;
  92. struct completion dma_rx_completion;
  93. char dma_rx_ch_name[14];
  94. char dma_tx_ch_name[14];
  95. };
  96. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  97. * cache operations; better heuristics consider wordsize and bitrate.
  98. */
  99. #define DMA_MIN_BYTES 160
  100. /*
  101. * Used for context save and restore, structure members to be updated whenever
  102. * corresponding registers are modified.
  103. */
  104. struct omap2_mcspi_regs {
  105. u32 modulctrl;
  106. u32 wakeupenable;
  107. struct list_head cs;
  108. };
  109. struct omap2_mcspi {
  110. struct spi_master *master;
  111. /* Virtual base address of the controller */
  112. void __iomem *base;
  113. unsigned long phys;
  114. /* SPI1 has 4 channels, while SPI2 has 2 */
  115. struct omap2_mcspi_dma *dma_channels;
  116. struct device *dev;
  117. struct omap2_mcspi_regs ctx;
  118. int fifo_depth;
  119. unsigned int pin_dir:1;
  120. };
  121. struct omap2_mcspi_cs {
  122. void __iomem *base;
  123. unsigned long phys;
  124. int word_len;
  125. u16 mode;
  126. struct list_head node;
  127. /* Context save and restore shadow register */
  128. u32 chconf0, chctrl0;
  129. };
  130. static inline void mcspi_write_reg(struct spi_master *master,
  131. int idx, u32 val)
  132. {
  133. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  134. writel_relaxed(val, mcspi->base + idx);
  135. }
  136. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  137. {
  138. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  139. return readl_relaxed(mcspi->base + idx);
  140. }
  141. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  142. int idx, u32 val)
  143. {
  144. struct omap2_mcspi_cs *cs = spi->controller_state;
  145. writel_relaxed(val, cs->base + idx);
  146. }
  147. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  148. {
  149. struct omap2_mcspi_cs *cs = spi->controller_state;
  150. return readl_relaxed(cs->base + idx);
  151. }
  152. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  153. {
  154. struct omap2_mcspi_cs *cs = spi->controller_state;
  155. return cs->chconf0;
  156. }
  157. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  158. {
  159. struct omap2_mcspi_cs *cs = spi->controller_state;
  160. cs->chconf0 = val;
  161. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  162. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  163. }
  164. static inline int mcspi_bytes_per_word(int word_len)
  165. {
  166. if (word_len <= 8)
  167. return 1;
  168. else if (word_len <= 16)
  169. return 2;
  170. else /* word_len <= 32 */
  171. return 4;
  172. }
  173. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  174. int is_read, int enable)
  175. {
  176. u32 l, rw;
  177. l = mcspi_cached_chconf0(spi);
  178. if (is_read) /* 1 is read, 0 write */
  179. rw = OMAP2_MCSPI_CHCONF_DMAR;
  180. else
  181. rw = OMAP2_MCSPI_CHCONF_DMAW;
  182. if (enable)
  183. l |= rw;
  184. else
  185. l &= ~rw;
  186. mcspi_write_chconf0(spi, l);
  187. }
  188. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  189. {
  190. struct omap2_mcspi_cs *cs = spi->controller_state;
  191. u32 l;
  192. l = cs->chctrl0;
  193. if (enable)
  194. l |= OMAP2_MCSPI_CHCTRL_EN;
  195. else
  196. l &= ~OMAP2_MCSPI_CHCTRL_EN;
  197. cs->chctrl0 = l;
  198. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  199. /* Flash post-writes */
  200. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  201. }
  202. static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
  203. {
  204. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  205. u32 l;
  206. /* The controller handles the inverted chip selects
  207. * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
  208. * the inversion from the core spi_set_cs function.
  209. */
  210. if (spi->mode & SPI_CS_HIGH)
  211. enable = !enable;
  212. if (spi->controller_state) {
  213. int err = pm_runtime_get_sync(mcspi->dev);
  214. if (err < 0) {
  215. pm_runtime_put_noidle(mcspi->dev);
  216. dev_err(mcspi->dev, "failed to get sync: %d\n", err);
  217. return;
  218. }
  219. l = mcspi_cached_chconf0(spi);
  220. if (enable)
  221. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  222. else
  223. l |= OMAP2_MCSPI_CHCONF_FORCE;
  224. mcspi_write_chconf0(spi, l);
  225. pm_runtime_mark_last_busy(mcspi->dev);
  226. pm_runtime_put_autosuspend(mcspi->dev);
  227. }
  228. }
  229. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  230. {
  231. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  232. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  233. u32 l;
  234. /*
  235. * Setup when switching from (reset default) slave mode
  236. * to single-channel master mode
  237. */
  238. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  239. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
  240. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  241. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  242. ctx->modulctrl = l;
  243. }
  244. static void omap2_mcspi_set_fifo(const struct spi_device *spi,
  245. struct spi_transfer *t, int enable)
  246. {
  247. struct spi_master *master = spi->master;
  248. struct omap2_mcspi_cs *cs = spi->controller_state;
  249. struct omap2_mcspi *mcspi;
  250. unsigned int wcnt;
  251. int max_fifo_depth, fifo_depth, bytes_per_word;
  252. u32 chconf, xferlevel;
  253. mcspi = spi_master_get_devdata(master);
  254. chconf = mcspi_cached_chconf0(spi);
  255. if (enable) {
  256. bytes_per_word = mcspi_bytes_per_word(cs->word_len);
  257. if (t->len % bytes_per_word != 0)
  258. goto disable_fifo;
  259. if (t->rx_buf != NULL && t->tx_buf != NULL)
  260. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
  261. else
  262. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
  263. fifo_depth = gcd(t->len, max_fifo_depth);
  264. if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
  265. goto disable_fifo;
  266. wcnt = t->len / bytes_per_word;
  267. if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
  268. goto disable_fifo;
  269. xferlevel = wcnt << 16;
  270. if (t->rx_buf != NULL) {
  271. chconf |= OMAP2_MCSPI_CHCONF_FFER;
  272. xferlevel |= (fifo_depth - 1) << 8;
  273. }
  274. if (t->tx_buf != NULL) {
  275. chconf |= OMAP2_MCSPI_CHCONF_FFET;
  276. xferlevel |= fifo_depth - 1;
  277. }
  278. mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
  279. mcspi_write_chconf0(spi, chconf);
  280. mcspi->fifo_depth = fifo_depth;
  281. return;
  282. }
  283. disable_fifo:
  284. if (t->rx_buf != NULL)
  285. chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
  286. if (t->tx_buf != NULL)
  287. chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
  288. mcspi_write_chconf0(spi, chconf);
  289. mcspi->fifo_depth = 0;
  290. }
  291. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  292. {
  293. unsigned long timeout;
  294. timeout = jiffies + msecs_to_jiffies(1000);
  295. while (!(readl_relaxed(reg) & bit)) {
  296. if (time_after(jiffies, timeout)) {
  297. if (!(readl_relaxed(reg) & bit))
  298. return -ETIMEDOUT;
  299. else
  300. return 0;
  301. }
  302. cpu_relax();
  303. }
  304. return 0;
  305. }
  306. static void omap2_mcspi_rx_callback(void *data)
  307. {
  308. struct spi_device *spi = data;
  309. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  310. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  311. /* We must disable the DMA RX request */
  312. omap2_mcspi_set_dma_req(spi, 1, 0);
  313. complete(&mcspi_dma->dma_rx_completion);
  314. }
  315. static void omap2_mcspi_tx_callback(void *data)
  316. {
  317. struct spi_device *spi = data;
  318. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  319. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  320. /* We must disable the DMA TX request */
  321. omap2_mcspi_set_dma_req(spi, 0, 0);
  322. complete(&mcspi_dma->dma_tx_completion);
  323. }
  324. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  325. struct spi_transfer *xfer,
  326. struct dma_slave_config cfg)
  327. {
  328. struct omap2_mcspi *mcspi;
  329. struct omap2_mcspi_dma *mcspi_dma;
  330. unsigned int count;
  331. mcspi = spi_master_get_devdata(spi->master);
  332. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  333. count = xfer->len;
  334. if (mcspi_dma->dma_tx) {
  335. struct dma_async_tx_descriptor *tx;
  336. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  337. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
  338. xfer->tx_sg.nents,
  339. DMA_MEM_TO_DEV,
  340. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  341. if (tx) {
  342. tx->callback = omap2_mcspi_tx_callback;
  343. tx->callback_param = spi;
  344. dmaengine_submit(tx);
  345. } else {
  346. /* FIXME: fall back to PIO? */
  347. }
  348. }
  349. dma_async_issue_pending(mcspi_dma->dma_tx);
  350. omap2_mcspi_set_dma_req(spi, 0, 1);
  351. }
  352. static unsigned
  353. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  354. struct dma_slave_config cfg,
  355. unsigned es)
  356. {
  357. struct omap2_mcspi *mcspi;
  358. struct omap2_mcspi_dma *mcspi_dma;
  359. unsigned int count, transfer_reduction = 0;
  360. struct scatterlist *sg_out[2];
  361. int nb_sizes = 0, out_mapped_nents[2], ret, x;
  362. size_t sizes[2];
  363. u32 l;
  364. int elements = 0;
  365. int word_len, element_count;
  366. struct omap2_mcspi_cs *cs = spi->controller_state;
  367. void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  368. mcspi = spi_master_get_devdata(spi->master);
  369. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  370. count = xfer->len;
  371. /*
  372. * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
  373. * it mentions reducing DMA transfer length by one element in master
  374. * normal mode.
  375. */
  376. if (mcspi->fifo_depth == 0)
  377. transfer_reduction = es;
  378. word_len = cs->word_len;
  379. l = mcspi_cached_chconf0(spi);
  380. if (word_len <= 8)
  381. element_count = count;
  382. else if (word_len <= 16)
  383. element_count = count >> 1;
  384. else /* word_len <= 32 */
  385. element_count = count >> 2;
  386. if (mcspi_dma->dma_rx) {
  387. struct dma_async_tx_descriptor *tx;
  388. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  389. /*
  390. * Reduce DMA transfer length by one more if McSPI is
  391. * configured in turbo mode.
  392. */
  393. if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
  394. transfer_reduction += es;
  395. if (transfer_reduction) {
  396. /* Split sgl into two. The second sgl won't be used. */
  397. sizes[0] = count - transfer_reduction;
  398. sizes[1] = transfer_reduction;
  399. nb_sizes = 2;
  400. } else {
  401. /*
  402. * Don't bother splitting the sgl. This essentially
  403. * clones the original sgl.
  404. */
  405. sizes[0] = count;
  406. nb_sizes = 1;
  407. }
  408. ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents,
  409. 0, nb_sizes,
  410. sizes,
  411. sg_out, out_mapped_nents,
  412. GFP_KERNEL);
  413. if (ret < 0) {
  414. dev_err(&spi->dev, "sg_split failed\n");
  415. return 0;
  416. }
  417. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx,
  418. sg_out[0],
  419. out_mapped_nents[0],
  420. DMA_DEV_TO_MEM,
  421. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  422. if (tx) {
  423. tx->callback = omap2_mcspi_rx_callback;
  424. tx->callback_param = spi;
  425. dmaengine_submit(tx);
  426. } else {
  427. /* FIXME: fall back to PIO? */
  428. }
  429. }
  430. dma_async_issue_pending(mcspi_dma->dma_rx);
  431. omap2_mcspi_set_dma_req(spi, 1, 1);
  432. wait_for_completion(&mcspi_dma->dma_rx_completion);
  433. for (x = 0; x < nb_sizes; x++)
  434. kfree(sg_out[x]);
  435. if (mcspi->fifo_depth > 0)
  436. return count;
  437. /*
  438. * Due to the DMA transfer length reduction the missing bytes must
  439. * be read manually to receive all of the expected data.
  440. */
  441. omap2_mcspi_set_enable(spi, 0);
  442. elements = element_count - 1;
  443. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  444. elements--;
  445. if (!mcspi_wait_for_reg_bit(chstat_reg,
  446. OMAP2_MCSPI_CHSTAT_RXS)) {
  447. u32 w;
  448. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  449. if (word_len <= 8)
  450. ((u8 *)xfer->rx_buf)[elements++] = w;
  451. else if (word_len <= 16)
  452. ((u16 *)xfer->rx_buf)[elements++] = w;
  453. else /* word_len <= 32 */
  454. ((u32 *)xfer->rx_buf)[elements++] = w;
  455. } else {
  456. int bytes_per_word = mcspi_bytes_per_word(word_len);
  457. dev_err(&spi->dev, "DMA RX penultimate word empty\n");
  458. count -= (bytes_per_word << 1);
  459. omap2_mcspi_set_enable(spi, 1);
  460. return count;
  461. }
  462. }
  463. if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
  464. u32 w;
  465. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  466. if (word_len <= 8)
  467. ((u8 *)xfer->rx_buf)[elements] = w;
  468. else if (word_len <= 16)
  469. ((u16 *)xfer->rx_buf)[elements] = w;
  470. else /* word_len <= 32 */
  471. ((u32 *)xfer->rx_buf)[elements] = w;
  472. } else {
  473. dev_err(&spi->dev, "DMA RX last word empty\n");
  474. count -= mcspi_bytes_per_word(word_len);
  475. }
  476. omap2_mcspi_set_enable(spi, 1);
  477. return count;
  478. }
  479. static unsigned
  480. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  481. {
  482. struct omap2_mcspi *mcspi;
  483. struct omap2_mcspi_cs *cs = spi->controller_state;
  484. struct omap2_mcspi_dma *mcspi_dma;
  485. unsigned int count;
  486. u32 l;
  487. u8 *rx;
  488. const u8 *tx;
  489. struct dma_slave_config cfg;
  490. enum dma_slave_buswidth width;
  491. unsigned es;
  492. u32 burst;
  493. void __iomem *chstat_reg;
  494. void __iomem *irqstat_reg;
  495. int wait_res;
  496. mcspi = spi_master_get_devdata(spi->master);
  497. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  498. l = mcspi_cached_chconf0(spi);
  499. if (cs->word_len <= 8) {
  500. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  501. es = 1;
  502. } else if (cs->word_len <= 16) {
  503. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  504. es = 2;
  505. } else {
  506. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  507. es = 4;
  508. }
  509. count = xfer->len;
  510. burst = 1;
  511. if (mcspi->fifo_depth > 0) {
  512. if (count > mcspi->fifo_depth)
  513. burst = mcspi->fifo_depth / es;
  514. else
  515. burst = count / es;
  516. }
  517. memset(&cfg, 0, sizeof(cfg));
  518. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  519. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  520. cfg.src_addr_width = width;
  521. cfg.dst_addr_width = width;
  522. cfg.src_maxburst = burst;
  523. cfg.dst_maxburst = burst;
  524. rx = xfer->rx_buf;
  525. tx = xfer->tx_buf;
  526. if (tx != NULL)
  527. omap2_mcspi_tx_dma(spi, xfer, cfg);
  528. if (rx != NULL)
  529. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  530. if (tx != NULL) {
  531. wait_for_completion(&mcspi_dma->dma_tx_completion);
  532. if (mcspi->fifo_depth > 0) {
  533. irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
  534. if (mcspi_wait_for_reg_bit(irqstat_reg,
  535. OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
  536. dev_err(&spi->dev, "EOW timed out\n");
  537. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
  538. OMAP2_MCSPI_IRQSTATUS_EOW);
  539. }
  540. /* for TX_ONLY mode, be sure all words have shifted out */
  541. if (rx == NULL) {
  542. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  543. if (mcspi->fifo_depth > 0) {
  544. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  545. OMAP2_MCSPI_CHSTAT_TXFFE);
  546. if (wait_res < 0)
  547. dev_err(&spi->dev, "TXFFE timed out\n");
  548. } else {
  549. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  550. OMAP2_MCSPI_CHSTAT_TXS);
  551. if (wait_res < 0)
  552. dev_err(&spi->dev, "TXS timed out\n");
  553. }
  554. if (wait_res >= 0 &&
  555. (mcspi_wait_for_reg_bit(chstat_reg,
  556. OMAP2_MCSPI_CHSTAT_EOT) < 0))
  557. dev_err(&spi->dev, "EOT timed out\n");
  558. }
  559. }
  560. return count;
  561. }
  562. static unsigned
  563. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  564. {
  565. struct omap2_mcspi *mcspi;
  566. struct omap2_mcspi_cs *cs = spi->controller_state;
  567. unsigned int count, c;
  568. u32 l;
  569. void __iomem *base = cs->base;
  570. void __iomem *tx_reg;
  571. void __iomem *rx_reg;
  572. void __iomem *chstat_reg;
  573. int word_len;
  574. mcspi = spi_master_get_devdata(spi->master);
  575. count = xfer->len;
  576. c = count;
  577. word_len = cs->word_len;
  578. l = mcspi_cached_chconf0(spi);
  579. /* We store the pre-calculated register addresses on stack to speed
  580. * up the transfer loop. */
  581. tx_reg = base + OMAP2_MCSPI_TX0;
  582. rx_reg = base + OMAP2_MCSPI_RX0;
  583. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  584. if (c < (word_len>>3))
  585. return 0;
  586. if (word_len <= 8) {
  587. u8 *rx;
  588. const u8 *tx;
  589. rx = xfer->rx_buf;
  590. tx = xfer->tx_buf;
  591. do {
  592. c -= 1;
  593. if (tx != NULL) {
  594. if (mcspi_wait_for_reg_bit(chstat_reg,
  595. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  596. dev_err(&spi->dev, "TXS timed out\n");
  597. goto out;
  598. }
  599. dev_vdbg(&spi->dev, "write-%d %02x\n",
  600. word_len, *tx);
  601. writel_relaxed(*tx++, tx_reg);
  602. }
  603. if (rx != NULL) {
  604. if (mcspi_wait_for_reg_bit(chstat_reg,
  605. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  606. dev_err(&spi->dev, "RXS timed out\n");
  607. goto out;
  608. }
  609. if (c == 1 && tx == NULL &&
  610. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  611. omap2_mcspi_set_enable(spi, 0);
  612. *rx++ = readl_relaxed(rx_reg);
  613. dev_vdbg(&spi->dev, "read-%d %02x\n",
  614. word_len, *(rx - 1));
  615. if (mcspi_wait_for_reg_bit(chstat_reg,
  616. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  617. dev_err(&spi->dev,
  618. "RXS timed out\n");
  619. goto out;
  620. }
  621. c = 0;
  622. } else if (c == 0 && tx == NULL) {
  623. omap2_mcspi_set_enable(spi, 0);
  624. }
  625. *rx++ = readl_relaxed(rx_reg);
  626. dev_vdbg(&spi->dev, "read-%d %02x\n",
  627. word_len, *(rx - 1));
  628. }
  629. } while (c);
  630. } else if (word_len <= 16) {
  631. u16 *rx;
  632. const u16 *tx;
  633. rx = xfer->rx_buf;
  634. tx = xfer->tx_buf;
  635. do {
  636. c -= 2;
  637. if (tx != NULL) {
  638. if (mcspi_wait_for_reg_bit(chstat_reg,
  639. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  640. dev_err(&spi->dev, "TXS timed out\n");
  641. goto out;
  642. }
  643. dev_vdbg(&spi->dev, "write-%d %04x\n",
  644. word_len, *tx);
  645. writel_relaxed(*tx++, tx_reg);
  646. }
  647. if (rx != NULL) {
  648. if (mcspi_wait_for_reg_bit(chstat_reg,
  649. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  650. dev_err(&spi->dev, "RXS timed out\n");
  651. goto out;
  652. }
  653. if (c == 2 && tx == NULL &&
  654. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  655. omap2_mcspi_set_enable(spi, 0);
  656. *rx++ = readl_relaxed(rx_reg);
  657. dev_vdbg(&spi->dev, "read-%d %04x\n",
  658. word_len, *(rx - 1));
  659. if (mcspi_wait_for_reg_bit(chstat_reg,
  660. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  661. dev_err(&spi->dev,
  662. "RXS timed out\n");
  663. goto out;
  664. }
  665. c = 0;
  666. } else if (c == 0 && tx == NULL) {
  667. omap2_mcspi_set_enable(spi, 0);
  668. }
  669. *rx++ = readl_relaxed(rx_reg);
  670. dev_vdbg(&spi->dev, "read-%d %04x\n",
  671. word_len, *(rx - 1));
  672. }
  673. } while (c >= 2);
  674. } else if (word_len <= 32) {
  675. u32 *rx;
  676. const u32 *tx;
  677. rx = xfer->rx_buf;
  678. tx = xfer->tx_buf;
  679. do {
  680. c -= 4;
  681. if (tx != NULL) {
  682. if (mcspi_wait_for_reg_bit(chstat_reg,
  683. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  684. dev_err(&spi->dev, "TXS timed out\n");
  685. goto out;
  686. }
  687. dev_vdbg(&spi->dev, "write-%d %08x\n",
  688. word_len, *tx);
  689. writel_relaxed(*tx++, tx_reg);
  690. }
  691. if (rx != NULL) {
  692. if (mcspi_wait_for_reg_bit(chstat_reg,
  693. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  694. dev_err(&spi->dev, "RXS timed out\n");
  695. goto out;
  696. }
  697. if (c == 4 && tx == NULL &&
  698. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  699. omap2_mcspi_set_enable(spi, 0);
  700. *rx++ = readl_relaxed(rx_reg);
  701. dev_vdbg(&spi->dev, "read-%d %08x\n",
  702. word_len, *(rx - 1));
  703. if (mcspi_wait_for_reg_bit(chstat_reg,
  704. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  705. dev_err(&spi->dev,
  706. "RXS timed out\n");
  707. goto out;
  708. }
  709. c = 0;
  710. } else if (c == 0 && tx == NULL) {
  711. omap2_mcspi_set_enable(spi, 0);
  712. }
  713. *rx++ = readl_relaxed(rx_reg);
  714. dev_vdbg(&spi->dev, "read-%d %08x\n",
  715. word_len, *(rx - 1));
  716. }
  717. } while (c >= 4);
  718. }
  719. /* for TX_ONLY mode, be sure all words have shifted out */
  720. if (xfer->rx_buf == NULL) {
  721. if (mcspi_wait_for_reg_bit(chstat_reg,
  722. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  723. dev_err(&spi->dev, "TXS timed out\n");
  724. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  725. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  726. dev_err(&spi->dev, "EOT timed out\n");
  727. /* disable chan to purge rx datas received in TX_ONLY transfer,
  728. * otherwise these rx datas will affect the direct following
  729. * RX_ONLY transfer.
  730. */
  731. omap2_mcspi_set_enable(spi, 0);
  732. }
  733. out:
  734. omap2_mcspi_set_enable(spi, 1);
  735. return count - c;
  736. }
  737. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  738. {
  739. u32 div;
  740. for (div = 0; div < 15; div++)
  741. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  742. return div;
  743. return 15;
  744. }
  745. /* called only when no transfer is active to this device */
  746. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  747. struct spi_transfer *t)
  748. {
  749. struct omap2_mcspi_cs *cs = spi->controller_state;
  750. struct omap2_mcspi *mcspi;
  751. struct spi_master *spi_cntrl;
  752. u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
  753. u8 word_len = spi->bits_per_word;
  754. u32 speed_hz = spi->max_speed_hz;
  755. mcspi = spi_master_get_devdata(spi->master);
  756. spi_cntrl = mcspi->master;
  757. if (t != NULL && t->bits_per_word)
  758. word_len = t->bits_per_word;
  759. cs->word_len = word_len;
  760. if (t && t->speed_hz)
  761. speed_hz = t->speed_hz;
  762. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  763. if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
  764. clkd = omap2_mcspi_calc_divisor(speed_hz);
  765. speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
  766. clkg = 0;
  767. } else {
  768. div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
  769. speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
  770. clkd = (div - 1) & 0xf;
  771. extclk = (div - 1) >> 4;
  772. clkg = OMAP2_MCSPI_CHCONF_CLKG;
  773. }
  774. l = mcspi_cached_chconf0(spi);
  775. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  776. * REVISIT: this controller could support SPI_3WIRE mode.
  777. */
  778. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  779. l &= ~OMAP2_MCSPI_CHCONF_IS;
  780. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  781. l |= OMAP2_MCSPI_CHCONF_DPE0;
  782. } else {
  783. l |= OMAP2_MCSPI_CHCONF_IS;
  784. l |= OMAP2_MCSPI_CHCONF_DPE1;
  785. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  786. }
  787. /* wordlength */
  788. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  789. l |= (word_len - 1) << 7;
  790. /* set chipselect polarity; manage with FORCE */
  791. if (!(spi->mode & SPI_CS_HIGH))
  792. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  793. else
  794. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  795. /* set clock divisor */
  796. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  797. l |= clkd << 2;
  798. /* set clock granularity */
  799. l &= ~OMAP2_MCSPI_CHCONF_CLKG;
  800. l |= clkg;
  801. if (clkg) {
  802. cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
  803. cs->chctrl0 |= extclk << 8;
  804. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  805. }
  806. /* set SPI mode 0..3 */
  807. if (spi->mode & SPI_CPOL)
  808. l |= OMAP2_MCSPI_CHCONF_POL;
  809. else
  810. l &= ~OMAP2_MCSPI_CHCONF_POL;
  811. if (spi->mode & SPI_CPHA)
  812. l |= OMAP2_MCSPI_CHCONF_PHA;
  813. else
  814. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  815. mcspi_write_chconf0(spi, l);
  816. cs->mode = spi->mode;
  817. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  818. speed_hz,
  819. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  820. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  821. return 0;
  822. }
  823. /*
  824. * Note that we currently allow DMA only if we get a channel
  825. * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
  826. */
  827. static int omap2_mcspi_request_dma(struct spi_device *spi)
  828. {
  829. struct spi_master *master = spi->master;
  830. struct omap2_mcspi *mcspi;
  831. struct omap2_mcspi_dma *mcspi_dma;
  832. int ret = 0;
  833. mcspi = spi_master_get_devdata(master);
  834. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  835. init_completion(&mcspi_dma->dma_rx_completion);
  836. init_completion(&mcspi_dma->dma_tx_completion);
  837. mcspi_dma->dma_rx = dma_request_chan(&master->dev,
  838. mcspi_dma->dma_rx_ch_name);
  839. if (IS_ERR(mcspi_dma->dma_rx)) {
  840. ret = PTR_ERR(mcspi_dma->dma_rx);
  841. mcspi_dma->dma_rx = NULL;
  842. goto no_dma;
  843. }
  844. mcspi_dma->dma_tx = dma_request_chan(&master->dev,
  845. mcspi_dma->dma_tx_ch_name);
  846. if (IS_ERR(mcspi_dma->dma_tx)) {
  847. ret = PTR_ERR(mcspi_dma->dma_tx);
  848. mcspi_dma->dma_tx = NULL;
  849. dma_release_channel(mcspi_dma->dma_rx);
  850. mcspi_dma->dma_rx = NULL;
  851. }
  852. no_dma:
  853. return ret;
  854. }
  855. static int omap2_mcspi_setup(struct spi_device *spi)
  856. {
  857. int ret;
  858. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  859. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  860. struct omap2_mcspi_dma *mcspi_dma;
  861. struct omap2_mcspi_cs *cs = spi->controller_state;
  862. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  863. if (!cs) {
  864. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  865. if (!cs)
  866. return -ENOMEM;
  867. cs->base = mcspi->base + spi->chip_select * 0x14;
  868. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  869. cs->mode = 0;
  870. cs->chconf0 = 0;
  871. cs->chctrl0 = 0;
  872. spi->controller_state = cs;
  873. /* Link this to context save list */
  874. list_add_tail(&cs->node, &ctx->cs);
  875. if (gpio_is_valid(spi->cs_gpio)) {
  876. ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
  877. if (ret) {
  878. dev_err(&spi->dev, "failed to request gpio\n");
  879. return ret;
  880. }
  881. gpio_direction_output(spi->cs_gpio,
  882. !(spi->mode & SPI_CS_HIGH));
  883. }
  884. }
  885. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  886. ret = omap2_mcspi_request_dma(spi);
  887. if (ret)
  888. dev_warn(&spi->dev, "not using DMA for McSPI (%d)\n",
  889. ret);
  890. }
  891. ret = pm_runtime_get_sync(mcspi->dev);
  892. if (ret < 0) {
  893. pm_runtime_put_noidle(mcspi->dev);
  894. return ret;
  895. }
  896. ret = omap2_mcspi_setup_transfer(spi, NULL);
  897. pm_runtime_mark_last_busy(mcspi->dev);
  898. pm_runtime_put_autosuspend(mcspi->dev);
  899. return ret;
  900. }
  901. static void omap2_mcspi_cleanup(struct spi_device *spi)
  902. {
  903. struct omap2_mcspi *mcspi;
  904. struct omap2_mcspi_dma *mcspi_dma;
  905. struct omap2_mcspi_cs *cs;
  906. mcspi = spi_master_get_devdata(spi->master);
  907. if (spi->controller_state) {
  908. /* Unlink controller state from context save list */
  909. cs = spi->controller_state;
  910. list_del(&cs->node);
  911. kfree(cs);
  912. }
  913. if (spi->chip_select < spi->master->num_chipselect) {
  914. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  915. if (mcspi_dma->dma_rx) {
  916. dma_release_channel(mcspi_dma->dma_rx);
  917. mcspi_dma->dma_rx = NULL;
  918. }
  919. if (mcspi_dma->dma_tx) {
  920. dma_release_channel(mcspi_dma->dma_tx);
  921. mcspi_dma->dma_tx = NULL;
  922. }
  923. }
  924. if (gpio_is_valid(spi->cs_gpio))
  925. gpio_free(spi->cs_gpio);
  926. }
  927. static int omap2_mcspi_transfer_one(struct spi_master *master,
  928. struct spi_device *spi,
  929. struct spi_transfer *t)
  930. {
  931. /* We only enable one channel at a time -- the one whose message is
  932. * -- although this controller would gladly
  933. * arbitrate among multiple channels. This corresponds to "single
  934. * channel" master mode. As a side effect, we need to manage the
  935. * chipselect with the FORCE bit ... CS != channel enable.
  936. */
  937. struct omap2_mcspi *mcspi;
  938. struct omap2_mcspi_dma *mcspi_dma;
  939. struct omap2_mcspi_cs *cs;
  940. struct omap2_mcspi_device_config *cd;
  941. int par_override = 0;
  942. int status = 0;
  943. u32 chconf;
  944. mcspi = spi_master_get_devdata(master);
  945. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  946. cs = spi->controller_state;
  947. cd = spi->controller_data;
  948. /*
  949. * The slave driver could have changed spi->mode in which case
  950. * it will be different from cs->mode (the current hardware setup).
  951. * If so, set par_override (even though its not a parity issue) so
  952. * omap2_mcspi_setup_transfer will be called to configure the hardware
  953. * with the correct mode on the first iteration of the loop below.
  954. */
  955. if (spi->mode != cs->mode)
  956. par_override = 1;
  957. omap2_mcspi_set_enable(spi, 0);
  958. if (gpio_is_valid(spi->cs_gpio))
  959. omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
  960. if (par_override ||
  961. (t->speed_hz != spi->max_speed_hz) ||
  962. (t->bits_per_word != spi->bits_per_word)) {
  963. par_override = 1;
  964. status = omap2_mcspi_setup_transfer(spi, t);
  965. if (status < 0)
  966. goto out;
  967. if (t->speed_hz == spi->max_speed_hz &&
  968. t->bits_per_word == spi->bits_per_word)
  969. par_override = 0;
  970. }
  971. if (cd && cd->cs_per_word) {
  972. chconf = mcspi->ctx.modulctrl;
  973. chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
  974. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  975. mcspi->ctx.modulctrl =
  976. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  977. }
  978. chconf = mcspi_cached_chconf0(spi);
  979. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  980. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  981. if (t->tx_buf == NULL)
  982. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  983. else if (t->rx_buf == NULL)
  984. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  985. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  986. /* Turbo mode is for more than one word */
  987. if (t->len > ((cs->word_len + 7) >> 3))
  988. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  989. }
  990. mcspi_write_chconf0(spi, chconf);
  991. if (t->len) {
  992. unsigned count;
  993. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  994. master->cur_msg_mapped &&
  995. master->can_dma(master, spi, t))
  996. omap2_mcspi_set_fifo(spi, t, 1);
  997. omap2_mcspi_set_enable(spi, 1);
  998. /* RX_ONLY mode needs dummy data in TX reg */
  999. if (t->tx_buf == NULL)
  1000. writel_relaxed(0, cs->base
  1001. + OMAP2_MCSPI_TX0);
  1002. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  1003. master->cur_msg_mapped &&
  1004. master->can_dma(master, spi, t))
  1005. count = omap2_mcspi_txrx_dma(spi, t);
  1006. else
  1007. count = omap2_mcspi_txrx_pio(spi, t);
  1008. if (count != t->len) {
  1009. status = -EIO;
  1010. goto out;
  1011. }
  1012. }
  1013. omap2_mcspi_set_enable(spi, 0);
  1014. if (mcspi->fifo_depth > 0)
  1015. omap2_mcspi_set_fifo(spi, t, 0);
  1016. out:
  1017. /* Restore defaults if they were overriden */
  1018. if (par_override) {
  1019. par_override = 0;
  1020. status = omap2_mcspi_setup_transfer(spi, NULL);
  1021. }
  1022. if (cd && cd->cs_per_word) {
  1023. chconf = mcspi->ctx.modulctrl;
  1024. chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  1025. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  1026. mcspi->ctx.modulctrl =
  1027. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1028. }
  1029. omap2_mcspi_set_enable(spi, 0);
  1030. if (gpio_is_valid(spi->cs_gpio))
  1031. omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
  1032. if (mcspi->fifo_depth > 0 && t)
  1033. omap2_mcspi_set_fifo(spi, t, 0);
  1034. return status;
  1035. }
  1036. static int omap2_mcspi_prepare_message(struct spi_master *master,
  1037. struct spi_message *msg)
  1038. {
  1039. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1040. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1041. struct omap2_mcspi_cs *cs;
  1042. /* Only a single channel can have the FORCE bit enabled
  1043. * in its chconf0 register.
  1044. * Scan all channels and disable them except the current one.
  1045. * A FORCE can remain from a last transfer having cs_change enabled
  1046. */
  1047. list_for_each_entry(cs, &ctx->cs, node) {
  1048. if (msg->spi->controller_state == cs)
  1049. continue;
  1050. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
  1051. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1052. writel_relaxed(cs->chconf0,
  1053. cs->base + OMAP2_MCSPI_CHCONF0);
  1054. readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
  1055. }
  1056. }
  1057. return 0;
  1058. }
  1059. static bool omap2_mcspi_can_dma(struct spi_master *master,
  1060. struct spi_device *spi,
  1061. struct spi_transfer *xfer)
  1062. {
  1063. return (xfer->len >= DMA_MIN_BYTES);
  1064. }
  1065. static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  1066. {
  1067. struct spi_master *master = mcspi->master;
  1068. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1069. int ret = 0;
  1070. ret = pm_runtime_get_sync(mcspi->dev);
  1071. if (ret < 0) {
  1072. pm_runtime_put_noidle(mcspi->dev);
  1073. return ret;
  1074. }
  1075. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  1076. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  1077. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  1078. omap2_mcspi_set_master_mode(master);
  1079. pm_runtime_mark_last_busy(mcspi->dev);
  1080. pm_runtime_put_autosuspend(mcspi->dev);
  1081. return 0;
  1082. }
  1083. /*
  1084. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1085. * inactive state when driver was suspend, then force it to inactive state at
  1086. * wake up.
  1087. */
  1088. static int omap_mcspi_runtime_resume(struct device *dev)
  1089. {
  1090. struct spi_master *master = dev_get_drvdata(dev);
  1091. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1092. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1093. struct omap2_mcspi_cs *cs;
  1094. /* McSPI: context restore */
  1095. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  1096. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  1097. list_for_each_entry(cs, &ctx->cs, node) {
  1098. /*
  1099. * We need to toggle CS state for OMAP take this
  1100. * change in account.
  1101. */
  1102. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1103. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1104. writel_relaxed(cs->chconf0,
  1105. cs->base + OMAP2_MCSPI_CHCONF0);
  1106. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1107. writel_relaxed(cs->chconf0,
  1108. cs->base + OMAP2_MCSPI_CHCONF0);
  1109. } else {
  1110. writel_relaxed(cs->chconf0,
  1111. cs->base + OMAP2_MCSPI_CHCONF0);
  1112. }
  1113. }
  1114. return 0;
  1115. }
  1116. static struct omap2_mcspi_platform_config omap2_pdata = {
  1117. .regs_offset = 0,
  1118. };
  1119. static struct omap2_mcspi_platform_config omap4_pdata = {
  1120. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1121. };
  1122. static const struct of_device_id omap_mcspi_of_match[] = {
  1123. {
  1124. .compatible = "ti,omap2-mcspi",
  1125. .data = &omap2_pdata,
  1126. },
  1127. {
  1128. .compatible = "ti,omap4-mcspi",
  1129. .data = &omap4_pdata,
  1130. },
  1131. { },
  1132. };
  1133. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  1134. static int omap2_mcspi_probe(struct platform_device *pdev)
  1135. {
  1136. struct spi_master *master;
  1137. const struct omap2_mcspi_platform_config *pdata;
  1138. struct omap2_mcspi *mcspi;
  1139. struct resource *r;
  1140. int status = 0, i;
  1141. u32 regs_offset = 0;
  1142. struct device_node *node = pdev->dev.of_node;
  1143. const struct of_device_id *match;
  1144. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  1145. if (master == NULL) {
  1146. dev_dbg(&pdev->dev, "master allocation failed\n");
  1147. return -ENOMEM;
  1148. }
  1149. /* the spi->mode bits understood by this driver: */
  1150. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1151. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1152. master->setup = omap2_mcspi_setup;
  1153. master->auto_runtime_pm = true;
  1154. master->prepare_message = omap2_mcspi_prepare_message;
  1155. master->can_dma = omap2_mcspi_can_dma;
  1156. master->transfer_one = omap2_mcspi_transfer_one;
  1157. master->set_cs = omap2_mcspi_set_cs;
  1158. master->cleanup = omap2_mcspi_cleanup;
  1159. master->dev.of_node = node;
  1160. master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
  1161. master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
  1162. platform_set_drvdata(pdev, master);
  1163. mcspi = spi_master_get_devdata(master);
  1164. mcspi->master = master;
  1165. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  1166. if (match) {
  1167. u32 num_cs = 1; /* default number of chipselect */
  1168. pdata = match->data;
  1169. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  1170. master->num_chipselect = num_cs;
  1171. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  1172. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  1173. } else {
  1174. pdata = dev_get_platdata(&pdev->dev);
  1175. master->num_chipselect = pdata->num_cs;
  1176. mcspi->pin_dir = pdata->pin_dir;
  1177. }
  1178. regs_offset = pdata->regs_offset;
  1179. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1180. mcspi->base = devm_ioremap_resource(&pdev->dev, r);
  1181. if (IS_ERR(mcspi->base)) {
  1182. status = PTR_ERR(mcspi->base);
  1183. goto free_master;
  1184. }
  1185. mcspi->phys = r->start + regs_offset;
  1186. mcspi->base += regs_offset;
  1187. mcspi->dev = &pdev->dev;
  1188. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1189. mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
  1190. sizeof(struct omap2_mcspi_dma),
  1191. GFP_KERNEL);
  1192. if (mcspi->dma_channels == NULL) {
  1193. status = -ENOMEM;
  1194. goto free_master;
  1195. }
  1196. for (i = 0; i < master->num_chipselect; i++) {
  1197. sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
  1198. sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
  1199. }
  1200. pm_runtime_use_autosuspend(&pdev->dev);
  1201. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1202. pm_runtime_enable(&pdev->dev);
  1203. status = omap2_mcspi_master_setup(mcspi);
  1204. if (status < 0)
  1205. goto disable_pm;
  1206. status = devm_spi_register_master(&pdev->dev, master);
  1207. if (status < 0)
  1208. goto disable_pm;
  1209. return status;
  1210. disable_pm:
  1211. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1212. pm_runtime_put_sync(&pdev->dev);
  1213. pm_runtime_disable(&pdev->dev);
  1214. free_master:
  1215. spi_master_put(master);
  1216. return status;
  1217. }
  1218. static int omap2_mcspi_remove(struct platform_device *pdev)
  1219. {
  1220. struct spi_master *master = platform_get_drvdata(pdev);
  1221. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1222. pm_runtime_dont_use_autosuspend(mcspi->dev);
  1223. pm_runtime_put_sync(mcspi->dev);
  1224. pm_runtime_disable(&pdev->dev);
  1225. return 0;
  1226. }
  1227. /* work with hotplug and coldplug */
  1228. MODULE_ALIAS("platform:omap2_mcspi");
  1229. #ifdef CONFIG_SUSPEND
  1230. static int omap2_mcspi_suspend_noirq(struct device *dev)
  1231. {
  1232. return pinctrl_pm_select_sleep_state(dev);
  1233. }
  1234. static int omap2_mcspi_resume_noirq(struct device *dev)
  1235. {
  1236. struct spi_master *master = dev_get_drvdata(dev);
  1237. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1238. int error;
  1239. error = pinctrl_pm_select_default_state(dev);
  1240. if (error)
  1241. dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
  1242. __func__, error);
  1243. return 0;
  1244. }
  1245. #else
  1246. #define omap2_mcspi_suspend_noirq NULL
  1247. #define omap2_mcspi_resume_noirq NULL
  1248. #endif
  1249. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1250. .suspend_noirq = omap2_mcspi_suspend_noirq,
  1251. .resume_noirq = omap2_mcspi_resume_noirq,
  1252. .runtime_resume = omap_mcspi_runtime_resume,
  1253. };
  1254. static struct platform_driver omap2_mcspi_driver = {
  1255. .driver = {
  1256. .name = "omap2_mcspi",
  1257. .pm = &omap2_mcspi_pm_ops,
  1258. .of_match_table = omap_mcspi_of_match,
  1259. },
  1260. .probe = omap2_mcspi_probe,
  1261. .remove = omap2_mcspi_remove,
  1262. };
  1263. module_platform_driver(omap2_mcspi_driver);
  1264. MODULE_LICENSE("GPL");