spi-omap2-mcspi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/spi/spi.h>
  39. #include <plat/dma.h>
  40. #include <plat/clock.h>
  41. #include <plat/mcspi.h>
  42. #define OMAP2_MCSPI_MAX_FREQ 48000000
  43. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  44. #define OMAP2_MCSPI_REVISION 0x00
  45. #define OMAP2_MCSPI_SYSSTATUS 0x14
  46. #define OMAP2_MCSPI_IRQSTATUS 0x18
  47. #define OMAP2_MCSPI_IRQENABLE 0x1c
  48. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  49. #define OMAP2_MCSPI_SYST 0x24
  50. #define OMAP2_MCSPI_MODULCTRL 0x28
  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_MODULCTRL_SINGLE BIT(0)
  59. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  60. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  61. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  62. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  63. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  64. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  65. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  66. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  67. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  68. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  69. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  70. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  71. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  72. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  73. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  74. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  75. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  76. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  77. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  78. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  79. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  80. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  81. /* We have 2 DMA channels per CS, one for RX and one for TX */
  82. struct omap2_mcspi_dma {
  83. int dma_tx_channel;
  84. int dma_rx_channel;
  85. int dma_tx_sync_dev;
  86. int dma_rx_sync_dev;
  87. struct completion dma_tx_completion;
  88. struct completion dma_rx_completion;
  89. };
  90. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  91. * cache operations; better heuristics consider wordsize and bitrate.
  92. */
  93. #define DMA_MIN_BYTES 160
  94. /*
  95. * Used for context save and restore, structure members to be updated whenever
  96. * corresponding registers are modified.
  97. */
  98. struct omap2_mcspi_regs {
  99. u32 modulctrl;
  100. u32 wakeupenable;
  101. struct list_head cs;
  102. };
  103. struct omap2_mcspi {
  104. struct spi_master *master;
  105. /* Virtual base address of the controller */
  106. void __iomem *base;
  107. unsigned long phys;
  108. /* SPI1 has 4 channels, while SPI2 has 2 */
  109. struct omap2_mcspi_dma *dma_channels;
  110. struct device *dev;
  111. struct omap2_mcspi_regs ctx;
  112. };
  113. struct omap2_mcspi_cs {
  114. void __iomem *base;
  115. unsigned long phys;
  116. int word_len;
  117. struct list_head node;
  118. /* Context save and restore shadow register */
  119. u32 chconf0;
  120. };
  121. #define MOD_REG_BIT(val, mask, set) do { \
  122. if (set) \
  123. val |= mask; \
  124. else \
  125. val &= ~mask; \
  126. } while (0)
  127. static inline void mcspi_write_reg(struct spi_master *master,
  128. int idx, u32 val)
  129. {
  130. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  131. __raw_writel(val, mcspi->base + idx);
  132. }
  133. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  134. {
  135. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  136. return __raw_readl(mcspi->base + idx);
  137. }
  138. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  139. int idx, u32 val)
  140. {
  141. struct omap2_mcspi_cs *cs = spi->controller_state;
  142. __raw_writel(val, cs->base + idx);
  143. }
  144. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  145. {
  146. struct omap2_mcspi_cs *cs = spi->controller_state;
  147. return __raw_readl(cs->base + idx);
  148. }
  149. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  150. {
  151. struct omap2_mcspi_cs *cs = spi->controller_state;
  152. return cs->chconf0;
  153. }
  154. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  155. {
  156. struct omap2_mcspi_cs *cs = spi->controller_state;
  157. cs->chconf0 = val;
  158. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  159. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  160. }
  161. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  162. int is_read, int enable)
  163. {
  164. u32 l, rw;
  165. l = mcspi_cached_chconf0(spi);
  166. if (is_read) /* 1 is read, 0 write */
  167. rw = OMAP2_MCSPI_CHCONF_DMAR;
  168. else
  169. rw = OMAP2_MCSPI_CHCONF_DMAW;
  170. MOD_REG_BIT(l, rw, enable);
  171. mcspi_write_chconf0(spi, l);
  172. }
  173. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  174. {
  175. u32 l;
  176. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  177. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  178. /* Flash post-writes */
  179. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  180. }
  181. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  182. {
  183. u32 l;
  184. l = mcspi_cached_chconf0(spi);
  185. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  186. mcspi_write_chconf0(spi, l);
  187. }
  188. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  189. {
  190. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  191. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  192. u32 l;
  193. /*
  194. * Setup when switching from (reset default) slave mode
  195. * to single-channel master mode
  196. */
  197. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  198. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  199. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  200. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  201. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  202. ctx->modulctrl = l;
  203. }
  204. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  205. {
  206. struct spi_master *spi_cntrl = mcspi->master;
  207. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  208. struct omap2_mcspi_cs *cs;
  209. /* McSPI: context restore */
  210. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  211. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  212. list_for_each_entry(cs, &ctx->cs, node)
  213. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  214. }
  215. static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
  216. {
  217. pm_runtime_mark_last_busy(mcspi->dev);
  218. pm_runtime_put_autosuspend(mcspi->dev);
  219. }
  220. static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
  221. {
  222. return pm_runtime_get_sync(mcspi->dev);
  223. }
  224. static int omap2_prepare_transfer(struct spi_master *master)
  225. {
  226. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  227. pm_runtime_get_sync(mcspi->dev);
  228. return 0;
  229. }
  230. static int omap2_unprepare_transfer(struct spi_master *master)
  231. {
  232. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  233. pm_runtime_mark_last_busy(mcspi->dev);
  234. pm_runtime_put_autosuspend(mcspi->dev);
  235. return 0;
  236. }
  237. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  238. {
  239. unsigned long timeout;
  240. timeout = jiffies + msecs_to_jiffies(1000);
  241. while (!(__raw_readl(reg) & bit)) {
  242. if (time_after(jiffies, timeout))
  243. return -1;
  244. cpu_relax();
  245. }
  246. return 0;
  247. }
  248. static unsigned
  249. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  250. {
  251. struct omap2_mcspi *mcspi;
  252. struct omap2_mcspi_cs *cs = spi->controller_state;
  253. struct omap2_mcspi_dma *mcspi_dma;
  254. unsigned int count, c;
  255. unsigned long base, tx_reg, rx_reg;
  256. int word_len, data_type, element_count;
  257. int elements = 0;
  258. u32 l;
  259. u8 * rx;
  260. const u8 * tx;
  261. void __iomem *chstat_reg;
  262. mcspi = spi_master_get_devdata(spi->master);
  263. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  264. l = mcspi_cached_chconf0(spi);
  265. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  266. count = xfer->len;
  267. c = count;
  268. word_len = cs->word_len;
  269. base = cs->phys;
  270. tx_reg = base + OMAP2_MCSPI_TX0;
  271. rx_reg = base + OMAP2_MCSPI_RX0;
  272. rx = xfer->rx_buf;
  273. tx = xfer->tx_buf;
  274. if (word_len <= 8) {
  275. data_type = OMAP_DMA_DATA_TYPE_S8;
  276. element_count = count;
  277. } else if (word_len <= 16) {
  278. data_type = OMAP_DMA_DATA_TYPE_S16;
  279. element_count = count >> 1;
  280. } else /* word_len <= 32 */ {
  281. data_type = OMAP_DMA_DATA_TYPE_S32;
  282. element_count = count >> 2;
  283. }
  284. if (tx != NULL) {
  285. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  286. data_type, element_count, 1,
  287. OMAP_DMA_SYNC_ELEMENT,
  288. mcspi_dma->dma_tx_sync_dev, 0);
  289. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  290. OMAP_DMA_AMODE_CONSTANT,
  291. tx_reg, 0, 0);
  292. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  293. OMAP_DMA_AMODE_POST_INC,
  294. xfer->tx_dma, 0, 0);
  295. }
  296. if (rx != NULL) {
  297. elements = element_count - 1;
  298. if (l & OMAP2_MCSPI_CHCONF_TURBO)
  299. elements--;
  300. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  301. data_type, elements, 1,
  302. OMAP_DMA_SYNC_ELEMENT,
  303. mcspi_dma->dma_rx_sync_dev, 1);
  304. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  305. OMAP_DMA_AMODE_CONSTANT,
  306. rx_reg, 0, 0);
  307. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  308. OMAP_DMA_AMODE_POST_INC,
  309. xfer->rx_dma, 0, 0);
  310. }
  311. if (tx != NULL) {
  312. omap_start_dma(mcspi_dma->dma_tx_channel);
  313. omap2_mcspi_set_dma_req(spi, 0, 1);
  314. }
  315. if (rx != NULL) {
  316. omap_start_dma(mcspi_dma->dma_rx_channel);
  317. omap2_mcspi_set_dma_req(spi, 1, 1);
  318. }
  319. if (tx != NULL) {
  320. wait_for_completion(&mcspi_dma->dma_tx_completion);
  321. dma_unmap_single(mcspi->dev, xfer->tx_dma, count,
  322. DMA_TO_DEVICE);
  323. /* for TX_ONLY mode, be sure all words have shifted out */
  324. if (rx == NULL) {
  325. if (mcspi_wait_for_reg_bit(chstat_reg,
  326. OMAP2_MCSPI_CHSTAT_TXS) < 0)
  327. dev_err(&spi->dev, "TXS timed out\n");
  328. else if (mcspi_wait_for_reg_bit(chstat_reg,
  329. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  330. dev_err(&spi->dev, "EOT timed out\n");
  331. }
  332. }
  333. if (rx != NULL) {
  334. wait_for_completion(&mcspi_dma->dma_rx_completion);
  335. dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
  336. DMA_FROM_DEVICE);
  337. omap2_mcspi_set_enable(spi, 0);
  338. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  339. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  340. & OMAP2_MCSPI_CHSTAT_RXS)) {
  341. u32 w;
  342. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  343. if (word_len <= 8)
  344. ((u8 *)xfer->rx_buf)[elements++] = w;
  345. else if (word_len <= 16)
  346. ((u16 *)xfer->rx_buf)[elements++] = w;
  347. else /* word_len <= 32 */
  348. ((u32 *)xfer->rx_buf)[elements++] = w;
  349. } else {
  350. dev_err(&spi->dev,
  351. "DMA RX penultimate word empty");
  352. count -= (word_len <= 8) ? 2 :
  353. (word_len <= 16) ? 4 :
  354. /* word_len <= 32 */ 8;
  355. omap2_mcspi_set_enable(spi, 1);
  356. return count;
  357. }
  358. }
  359. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  360. & OMAP2_MCSPI_CHSTAT_RXS)) {
  361. u32 w;
  362. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  363. if (word_len <= 8)
  364. ((u8 *)xfer->rx_buf)[elements] = w;
  365. else if (word_len <= 16)
  366. ((u16 *)xfer->rx_buf)[elements] = w;
  367. else /* word_len <= 32 */
  368. ((u32 *)xfer->rx_buf)[elements] = w;
  369. } else {
  370. dev_err(&spi->dev, "DMA RX last word empty");
  371. count -= (word_len <= 8) ? 1 :
  372. (word_len <= 16) ? 2 :
  373. /* word_len <= 32 */ 4;
  374. }
  375. omap2_mcspi_set_enable(spi, 1);
  376. }
  377. return count;
  378. }
  379. static unsigned
  380. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  381. {
  382. struct omap2_mcspi *mcspi;
  383. struct omap2_mcspi_cs *cs = spi->controller_state;
  384. unsigned int count, c;
  385. u32 l;
  386. void __iomem *base = cs->base;
  387. void __iomem *tx_reg;
  388. void __iomem *rx_reg;
  389. void __iomem *chstat_reg;
  390. int word_len;
  391. mcspi = spi_master_get_devdata(spi->master);
  392. count = xfer->len;
  393. c = count;
  394. word_len = cs->word_len;
  395. l = mcspi_cached_chconf0(spi);
  396. /* We store the pre-calculated register addresses on stack to speed
  397. * up the transfer loop. */
  398. tx_reg = base + OMAP2_MCSPI_TX0;
  399. rx_reg = base + OMAP2_MCSPI_RX0;
  400. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  401. if (c < (word_len>>3))
  402. return 0;
  403. if (word_len <= 8) {
  404. u8 *rx;
  405. const u8 *tx;
  406. rx = xfer->rx_buf;
  407. tx = xfer->tx_buf;
  408. do {
  409. c -= 1;
  410. if (tx != NULL) {
  411. if (mcspi_wait_for_reg_bit(chstat_reg,
  412. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  413. dev_err(&spi->dev, "TXS timed out\n");
  414. goto out;
  415. }
  416. dev_vdbg(&spi->dev, "write-%d %02x\n",
  417. word_len, *tx);
  418. __raw_writel(*tx++, tx_reg);
  419. }
  420. if (rx != NULL) {
  421. if (mcspi_wait_for_reg_bit(chstat_reg,
  422. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  423. dev_err(&spi->dev, "RXS timed out\n");
  424. goto out;
  425. }
  426. if (c == 1 && tx == NULL &&
  427. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  428. omap2_mcspi_set_enable(spi, 0);
  429. *rx++ = __raw_readl(rx_reg);
  430. dev_vdbg(&spi->dev, "read-%d %02x\n",
  431. word_len, *(rx - 1));
  432. if (mcspi_wait_for_reg_bit(chstat_reg,
  433. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  434. dev_err(&spi->dev,
  435. "RXS timed out\n");
  436. goto out;
  437. }
  438. c = 0;
  439. } else if (c == 0 && tx == NULL) {
  440. omap2_mcspi_set_enable(spi, 0);
  441. }
  442. *rx++ = __raw_readl(rx_reg);
  443. dev_vdbg(&spi->dev, "read-%d %02x\n",
  444. word_len, *(rx - 1));
  445. }
  446. } while (c);
  447. } else if (word_len <= 16) {
  448. u16 *rx;
  449. const u16 *tx;
  450. rx = xfer->rx_buf;
  451. tx = xfer->tx_buf;
  452. do {
  453. c -= 2;
  454. if (tx != NULL) {
  455. if (mcspi_wait_for_reg_bit(chstat_reg,
  456. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  457. dev_err(&spi->dev, "TXS timed out\n");
  458. goto out;
  459. }
  460. dev_vdbg(&spi->dev, "write-%d %04x\n",
  461. word_len, *tx);
  462. __raw_writel(*tx++, tx_reg);
  463. }
  464. if (rx != NULL) {
  465. if (mcspi_wait_for_reg_bit(chstat_reg,
  466. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  467. dev_err(&spi->dev, "RXS timed out\n");
  468. goto out;
  469. }
  470. if (c == 2 && tx == NULL &&
  471. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  472. omap2_mcspi_set_enable(spi, 0);
  473. *rx++ = __raw_readl(rx_reg);
  474. dev_vdbg(&spi->dev, "read-%d %04x\n",
  475. word_len, *(rx - 1));
  476. if (mcspi_wait_for_reg_bit(chstat_reg,
  477. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  478. dev_err(&spi->dev,
  479. "RXS timed out\n");
  480. goto out;
  481. }
  482. c = 0;
  483. } else if (c == 0 && tx == NULL) {
  484. omap2_mcspi_set_enable(spi, 0);
  485. }
  486. *rx++ = __raw_readl(rx_reg);
  487. dev_vdbg(&spi->dev, "read-%d %04x\n",
  488. word_len, *(rx - 1));
  489. }
  490. } while (c >= 2);
  491. } else if (word_len <= 32) {
  492. u32 *rx;
  493. const u32 *tx;
  494. rx = xfer->rx_buf;
  495. tx = xfer->tx_buf;
  496. do {
  497. c -= 4;
  498. if (tx != NULL) {
  499. if (mcspi_wait_for_reg_bit(chstat_reg,
  500. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  501. dev_err(&spi->dev, "TXS timed out\n");
  502. goto out;
  503. }
  504. dev_vdbg(&spi->dev, "write-%d %08x\n",
  505. word_len, *tx);
  506. __raw_writel(*tx++, tx_reg);
  507. }
  508. if (rx != NULL) {
  509. if (mcspi_wait_for_reg_bit(chstat_reg,
  510. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  511. dev_err(&spi->dev, "RXS timed out\n");
  512. goto out;
  513. }
  514. if (c == 4 && tx == NULL &&
  515. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  516. omap2_mcspi_set_enable(spi, 0);
  517. *rx++ = __raw_readl(rx_reg);
  518. dev_vdbg(&spi->dev, "read-%d %08x\n",
  519. word_len, *(rx - 1));
  520. if (mcspi_wait_for_reg_bit(chstat_reg,
  521. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  522. dev_err(&spi->dev,
  523. "RXS timed out\n");
  524. goto out;
  525. }
  526. c = 0;
  527. } else if (c == 0 && tx == NULL) {
  528. omap2_mcspi_set_enable(spi, 0);
  529. }
  530. *rx++ = __raw_readl(rx_reg);
  531. dev_vdbg(&spi->dev, "read-%d %08x\n",
  532. word_len, *(rx - 1));
  533. }
  534. } while (c >= 4);
  535. }
  536. /* for TX_ONLY mode, be sure all words have shifted out */
  537. if (xfer->rx_buf == NULL) {
  538. if (mcspi_wait_for_reg_bit(chstat_reg,
  539. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  540. dev_err(&spi->dev, "TXS timed out\n");
  541. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  542. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  543. dev_err(&spi->dev, "EOT timed out\n");
  544. /* disable chan to purge rx datas received in TX_ONLY transfer,
  545. * otherwise these rx datas will affect the direct following
  546. * RX_ONLY transfer.
  547. */
  548. omap2_mcspi_set_enable(spi, 0);
  549. }
  550. out:
  551. omap2_mcspi_set_enable(spi, 1);
  552. return count - c;
  553. }
  554. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  555. {
  556. u32 div;
  557. for (div = 0; div < 15; div++)
  558. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  559. return div;
  560. return 15;
  561. }
  562. /* called only when no transfer is active to this device */
  563. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  564. struct spi_transfer *t)
  565. {
  566. struct omap2_mcspi_cs *cs = spi->controller_state;
  567. struct omap2_mcspi *mcspi;
  568. struct spi_master *spi_cntrl;
  569. u32 l = 0, div = 0;
  570. u8 word_len = spi->bits_per_word;
  571. u32 speed_hz = spi->max_speed_hz;
  572. mcspi = spi_master_get_devdata(spi->master);
  573. spi_cntrl = mcspi->master;
  574. if (t != NULL && t->bits_per_word)
  575. word_len = t->bits_per_word;
  576. cs->word_len = word_len;
  577. if (t && t->speed_hz)
  578. speed_hz = t->speed_hz;
  579. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  580. div = omap2_mcspi_calc_divisor(speed_hz);
  581. l = mcspi_cached_chconf0(spi);
  582. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  583. * REVISIT: this controller could support SPI_3WIRE mode.
  584. */
  585. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  586. l |= OMAP2_MCSPI_CHCONF_DPE0;
  587. /* wordlength */
  588. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  589. l |= (word_len - 1) << 7;
  590. /* set chipselect polarity; manage with FORCE */
  591. if (!(spi->mode & SPI_CS_HIGH))
  592. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  593. else
  594. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  595. /* set clock divisor */
  596. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  597. l |= div << 2;
  598. /* set SPI mode 0..3 */
  599. if (spi->mode & SPI_CPOL)
  600. l |= OMAP2_MCSPI_CHCONF_POL;
  601. else
  602. l &= ~OMAP2_MCSPI_CHCONF_POL;
  603. if (spi->mode & SPI_CPHA)
  604. l |= OMAP2_MCSPI_CHCONF_PHA;
  605. else
  606. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  607. mcspi_write_chconf0(spi, l);
  608. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  609. OMAP2_MCSPI_MAX_FREQ >> div,
  610. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  611. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  612. return 0;
  613. }
  614. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  615. {
  616. struct spi_device *spi = data;
  617. struct omap2_mcspi *mcspi;
  618. struct omap2_mcspi_dma *mcspi_dma;
  619. mcspi = spi_master_get_devdata(spi->master);
  620. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  621. complete(&mcspi_dma->dma_rx_completion);
  622. /* We must disable the DMA RX request */
  623. omap2_mcspi_set_dma_req(spi, 1, 0);
  624. }
  625. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  626. {
  627. struct spi_device *spi = data;
  628. struct omap2_mcspi *mcspi;
  629. struct omap2_mcspi_dma *mcspi_dma;
  630. mcspi = spi_master_get_devdata(spi->master);
  631. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  632. complete(&mcspi_dma->dma_tx_completion);
  633. /* We must disable the DMA TX request */
  634. omap2_mcspi_set_dma_req(spi, 0, 0);
  635. }
  636. static int omap2_mcspi_request_dma(struct spi_device *spi)
  637. {
  638. struct spi_master *master = spi->master;
  639. struct omap2_mcspi *mcspi;
  640. struct omap2_mcspi_dma *mcspi_dma;
  641. mcspi = spi_master_get_devdata(master);
  642. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  643. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  644. omap2_mcspi_dma_rx_callback, spi,
  645. &mcspi_dma->dma_rx_channel)) {
  646. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  647. return -EAGAIN;
  648. }
  649. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  650. omap2_mcspi_dma_tx_callback, spi,
  651. &mcspi_dma->dma_tx_channel)) {
  652. omap_free_dma(mcspi_dma->dma_rx_channel);
  653. mcspi_dma->dma_rx_channel = -1;
  654. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  655. return -EAGAIN;
  656. }
  657. init_completion(&mcspi_dma->dma_rx_completion);
  658. init_completion(&mcspi_dma->dma_tx_completion);
  659. return 0;
  660. }
  661. static int omap2_mcspi_setup(struct spi_device *spi)
  662. {
  663. int ret;
  664. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  665. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  666. struct omap2_mcspi_dma *mcspi_dma;
  667. struct omap2_mcspi_cs *cs = spi->controller_state;
  668. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  669. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  670. spi->bits_per_word);
  671. return -EINVAL;
  672. }
  673. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  674. if (!cs) {
  675. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  676. if (!cs)
  677. return -ENOMEM;
  678. cs->base = mcspi->base + spi->chip_select * 0x14;
  679. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  680. cs->chconf0 = 0;
  681. spi->controller_state = cs;
  682. /* Link this to context save list */
  683. list_add_tail(&cs->node, &ctx->cs);
  684. }
  685. if (mcspi_dma->dma_rx_channel == -1
  686. || mcspi_dma->dma_tx_channel == -1) {
  687. ret = omap2_mcspi_request_dma(spi);
  688. if (ret < 0)
  689. return ret;
  690. }
  691. ret = omap2_mcspi_enable_clocks(mcspi);
  692. if (ret < 0)
  693. return ret;
  694. ret = omap2_mcspi_setup_transfer(spi, NULL);
  695. omap2_mcspi_disable_clocks(mcspi);
  696. return ret;
  697. }
  698. static void omap2_mcspi_cleanup(struct spi_device *spi)
  699. {
  700. struct omap2_mcspi *mcspi;
  701. struct omap2_mcspi_dma *mcspi_dma;
  702. struct omap2_mcspi_cs *cs;
  703. mcspi = spi_master_get_devdata(spi->master);
  704. if (spi->controller_state) {
  705. /* Unlink controller state from context save list */
  706. cs = spi->controller_state;
  707. list_del(&cs->node);
  708. kfree(cs);
  709. }
  710. if (spi->chip_select < spi->master->num_chipselect) {
  711. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  712. if (mcspi_dma->dma_rx_channel != -1) {
  713. omap_free_dma(mcspi_dma->dma_rx_channel);
  714. mcspi_dma->dma_rx_channel = -1;
  715. }
  716. if (mcspi_dma->dma_tx_channel != -1) {
  717. omap_free_dma(mcspi_dma->dma_tx_channel);
  718. mcspi_dma->dma_tx_channel = -1;
  719. }
  720. }
  721. }
  722. static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
  723. {
  724. /* We only enable one channel at a time -- the one whose message is
  725. * -- although this controller would gladly
  726. * arbitrate among multiple channels. This corresponds to "single
  727. * channel" master mode. As a side effect, we need to manage the
  728. * chipselect with the FORCE bit ... CS != channel enable.
  729. */
  730. struct spi_device *spi;
  731. struct spi_transfer *t = NULL;
  732. int cs_active = 0;
  733. struct omap2_mcspi_cs *cs;
  734. struct omap2_mcspi_device_config *cd;
  735. int par_override = 0;
  736. int status = 0;
  737. u32 chconf;
  738. spi = m->spi;
  739. cs = spi->controller_state;
  740. cd = spi->controller_data;
  741. omap2_mcspi_set_enable(spi, 1);
  742. list_for_each_entry(t, &m->transfers, transfer_list) {
  743. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  744. status = -EINVAL;
  745. break;
  746. }
  747. if (par_override || t->speed_hz || t->bits_per_word) {
  748. par_override = 1;
  749. status = omap2_mcspi_setup_transfer(spi, t);
  750. if (status < 0)
  751. break;
  752. if (!t->speed_hz && !t->bits_per_word)
  753. par_override = 0;
  754. }
  755. if (!cs_active) {
  756. omap2_mcspi_force_cs(spi, 1);
  757. cs_active = 1;
  758. }
  759. chconf = mcspi_cached_chconf0(spi);
  760. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  761. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  762. if (t->tx_buf == NULL)
  763. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  764. else if (t->rx_buf == NULL)
  765. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  766. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  767. /* Turbo mode is for more than one word */
  768. if (t->len > ((cs->word_len + 7) >> 3))
  769. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  770. }
  771. mcspi_write_chconf0(spi, chconf);
  772. if (t->len) {
  773. unsigned count;
  774. /* RX_ONLY mode needs dummy data in TX reg */
  775. if (t->tx_buf == NULL)
  776. __raw_writel(0, cs->base
  777. + OMAP2_MCSPI_TX0);
  778. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  779. count = omap2_mcspi_txrx_dma(spi, t);
  780. else
  781. count = omap2_mcspi_txrx_pio(spi, t);
  782. m->actual_length += count;
  783. if (count != t->len) {
  784. status = -EIO;
  785. break;
  786. }
  787. }
  788. if (t->delay_usecs)
  789. udelay(t->delay_usecs);
  790. /* ignore the "leave it on after last xfer" hint */
  791. if (t->cs_change) {
  792. omap2_mcspi_force_cs(spi, 0);
  793. cs_active = 0;
  794. }
  795. }
  796. /* Restore defaults if they were overriden */
  797. if (par_override) {
  798. par_override = 0;
  799. status = omap2_mcspi_setup_transfer(spi, NULL);
  800. }
  801. if (cs_active)
  802. omap2_mcspi_force_cs(spi, 0);
  803. omap2_mcspi_set_enable(spi, 0);
  804. m->status = status;
  805. }
  806. static int omap2_mcspi_transfer_one_message(struct spi_master *master,
  807. struct spi_message *m)
  808. {
  809. struct omap2_mcspi *mcspi;
  810. struct spi_transfer *t;
  811. mcspi = spi_master_get_devdata(master);
  812. m->actual_length = 0;
  813. m->status = 0;
  814. /* reject invalid messages and transfers */
  815. if (list_empty(&m->transfers))
  816. return -EINVAL;
  817. list_for_each_entry(t, &m->transfers, transfer_list) {
  818. const void *tx_buf = t->tx_buf;
  819. void *rx_buf = t->rx_buf;
  820. unsigned len = t->len;
  821. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  822. || (len && !(rx_buf || tx_buf))
  823. || (t->bits_per_word &&
  824. ( t->bits_per_word < 4
  825. || t->bits_per_word > 32))) {
  826. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  827. t->speed_hz,
  828. len,
  829. tx_buf ? "tx" : "",
  830. rx_buf ? "rx" : "",
  831. t->bits_per_word);
  832. return -EINVAL;
  833. }
  834. if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
  835. dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
  836. t->speed_hz,
  837. OMAP2_MCSPI_MAX_FREQ >> 15);
  838. return -EINVAL;
  839. }
  840. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  841. continue;
  842. if (tx_buf != NULL) {
  843. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  844. len, DMA_TO_DEVICE);
  845. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  846. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  847. 'T', len);
  848. return -EINVAL;
  849. }
  850. }
  851. if (rx_buf != NULL) {
  852. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  853. DMA_FROM_DEVICE);
  854. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  855. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  856. 'R', len);
  857. if (tx_buf != NULL)
  858. dma_unmap_single(mcspi->dev, t->tx_dma,
  859. len, DMA_TO_DEVICE);
  860. return -EINVAL;
  861. }
  862. }
  863. }
  864. omap2_mcspi_work(mcspi, m);
  865. spi_finalize_current_message(master);
  866. return 0;
  867. }
  868. static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  869. {
  870. struct spi_master *master = mcspi->master;
  871. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  872. int ret = 0;
  873. ret = omap2_mcspi_enable_clocks(mcspi);
  874. if (ret < 0)
  875. return ret;
  876. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  877. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  878. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  879. omap2_mcspi_set_master_mode(master);
  880. omap2_mcspi_disable_clocks(mcspi);
  881. return 0;
  882. }
  883. static int omap_mcspi_runtime_resume(struct device *dev)
  884. {
  885. struct omap2_mcspi *mcspi;
  886. struct spi_master *master;
  887. master = dev_get_drvdata(dev);
  888. mcspi = spi_master_get_devdata(master);
  889. omap2_mcspi_restore_ctx(mcspi);
  890. return 0;
  891. }
  892. static struct omap2_mcspi_platform_config omap2_pdata = {
  893. .regs_offset = 0,
  894. };
  895. static struct omap2_mcspi_platform_config omap4_pdata = {
  896. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  897. };
  898. static const struct of_device_id omap_mcspi_of_match[] = {
  899. {
  900. .compatible = "ti,omap2-mcspi",
  901. .data = &omap2_pdata,
  902. },
  903. {
  904. .compatible = "ti,omap4-mcspi",
  905. .data = &omap4_pdata,
  906. },
  907. { },
  908. };
  909. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  910. static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
  911. {
  912. struct spi_master *master;
  913. struct omap2_mcspi_platform_config *pdata;
  914. struct omap2_mcspi *mcspi;
  915. struct resource *r;
  916. int status = 0, i;
  917. u32 regs_offset = 0;
  918. static int bus_num = 1;
  919. struct device_node *node = pdev->dev.of_node;
  920. const struct of_device_id *match;
  921. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  922. if (master == NULL) {
  923. dev_dbg(&pdev->dev, "master allocation failed\n");
  924. return -ENOMEM;
  925. }
  926. /* the spi->mode bits understood by this driver: */
  927. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  928. master->setup = omap2_mcspi_setup;
  929. master->prepare_transfer_hardware = omap2_prepare_transfer;
  930. master->unprepare_transfer_hardware = omap2_unprepare_transfer;
  931. master->transfer_one_message = omap2_mcspi_transfer_one_message;
  932. master->cleanup = omap2_mcspi_cleanup;
  933. master->dev.of_node = node;
  934. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  935. if (match) {
  936. u32 num_cs = 1; /* default number of chipselect */
  937. pdata = match->data;
  938. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  939. master->num_chipselect = num_cs;
  940. master->bus_num = bus_num++;
  941. } else {
  942. pdata = pdev->dev.platform_data;
  943. master->num_chipselect = pdata->num_cs;
  944. if (pdev->id != -1)
  945. master->bus_num = pdev->id;
  946. }
  947. regs_offset = pdata->regs_offset;
  948. dev_set_drvdata(&pdev->dev, master);
  949. mcspi = spi_master_get_devdata(master);
  950. mcspi->master = master;
  951. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  952. if (r == NULL) {
  953. status = -ENODEV;
  954. goto free_master;
  955. }
  956. r->start += regs_offset;
  957. r->end += regs_offset;
  958. mcspi->phys = r->start;
  959. mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
  960. if (!mcspi->base) {
  961. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  962. status = -ENOMEM;
  963. goto free_master;
  964. }
  965. mcspi->dev = &pdev->dev;
  966. INIT_LIST_HEAD(&mcspi->ctx.cs);
  967. mcspi->dma_channels = kcalloc(master->num_chipselect,
  968. sizeof(struct omap2_mcspi_dma),
  969. GFP_KERNEL);
  970. if (mcspi->dma_channels == NULL)
  971. goto free_master;
  972. for (i = 0; i < master->num_chipselect; i++) {
  973. char dma_ch_name[14];
  974. struct resource *dma_res;
  975. sprintf(dma_ch_name, "rx%d", i);
  976. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  977. dma_ch_name);
  978. if (!dma_res) {
  979. dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
  980. status = -ENODEV;
  981. break;
  982. }
  983. mcspi->dma_channels[i].dma_rx_channel = -1;
  984. mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
  985. sprintf(dma_ch_name, "tx%d", i);
  986. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  987. dma_ch_name);
  988. if (!dma_res) {
  989. dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
  990. status = -ENODEV;
  991. break;
  992. }
  993. mcspi->dma_channels[i].dma_tx_channel = -1;
  994. mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
  995. }
  996. if (status < 0)
  997. goto dma_chnl_free;
  998. pm_runtime_use_autosuspend(&pdev->dev);
  999. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1000. pm_runtime_enable(&pdev->dev);
  1001. if (status || omap2_mcspi_master_setup(mcspi) < 0)
  1002. goto disable_pm;
  1003. status = spi_register_master(master);
  1004. if (status < 0)
  1005. goto err_spi_register;
  1006. return status;
  1007. err_spi_register:
  1008. spi_master_put(master);
  1009. disable_pm:
  1010. pm_runtime_disable(&pdev->dev);
  1011. dma_chnl_free:
  1012. kfree(mcspi->dma_channels);
  1013. free_master:
  1014. kfree(master);
  1015. platform_set_drvdata(pdev, NULL);
  1016. return status;
  1017. }
  1018. static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
  1019. {
  1020. struct spi_master *master;
  1021. struct omap2_mcspi *mcspi;
  1022. struct omap2_mcspi_dma *dma_channels;
  1023. master = dev_get_drvdata(&pdev->dev);
  1024. mcspi = spi_master_get_devdata(master);
  1025. dma_channels = mcspi->dma_channels;
  1026. omap2_mcspi_disable_clocks(mcspi);
  1027. pm_runtime_disable(&pdev->dev);
  1028. spi_unregister_master(master);
  1029. kfree(dma_channels);
  1030. platform_set_drvdata(pdev, NULL);
  1031. return 0;
  1032. }
  1033. /* work with hotplug and coldplug */
  1034. MODULE_ALIAS("platform:omap2_mcspi");
  1035. #ifdef CONFIG_SUSPEND
  1036. /*
  1037. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1038. * unactive state when driver was suspend, then force it to unactive state at
  1039. * wake up.
  1040. */
  1041. static int omap2_mcspi_resume(struct device *dev)
  1042. {
  1043. struct spi_master *master = dev_get_drvdata(dev);
  1044. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1045. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1046. struct omap2_mcspi_cs *cs;
  1047. omap2_mcspi_enable_clocks(mcspi);
  1048. list_for_each_entry(cs, &ctx->cs, node) {
  1049. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1050. /*
  1051. * We need to toggle CS state for OMAP take this
  1052. * change in account.
  1053. */
  1054. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
  1055. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1056. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
  1057. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1058. }
  1059. }
  1060. omap2_mcspi_disable_clocks(mcspi);
  1061. return 0;
  1062. }
  1063. #else
  1064. #define omap2_mcspi_resume NULL
  1065. #endif
  1066. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1067. .resume = omap2_mcspi_resume,
  1068. .runtime_resume = omap_mcspi_runtime_resume,
  1069. };
  1070. static struct platform_driver omap2_mcspi_driver = {
  1071. .driver = {
  1072. .name = "omap2_mcspi",
  1073. .owner = THIS_MODULE,
  1074. .pm = &omap2_mcspi_pm_ops,
  1075. .of_match_table = omap_mcspi_of_match,
  1076. },
  1077. .probe = omap2_mcspi_probe,
  1078. .remove = __devexit_p(omap2_mcspi_remove),
  1079. };
  1080. module_platform_driver(omap2_mcspi_driver);
  1081. MODULE_LICENSE("GPL");