spi-omap2-mcspi.c 38 KB

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