spi-tegra20-slink.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/completion.h>
  9. #include <linux/delay.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kthread.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_opp.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/reset.h>
  25. #include <linux/spi/spi.h>
  26. #include <soc/tegra/common.h>
  27. #define SLINK_COMMAND 0x000
  28. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  29. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  30. #define SLINK_BOTH_EN (1 << 10)
  31. #define SLINK_CS_SW (1 << 11)
  32. #define SLINK_CS_VALUE (1 << 12)
  33. #define SLINK_CS_POLARITY (1 << 13)
  34. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  35. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  36. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  37. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  38. #define SLINK_IDLE_SDA_MASK (3 << 16)
  39. #define SLINK_CS_POLARITY1 (1 << 20)
  40. #define SLINK_CK_SDA (1 << 21)
  41. #define SLINK_CS_POLARITY2 (1 << 22)
  42. #define SLINK_CS_POLARITY3 (1 << 23)
  43. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  44. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  45. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  46. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  47. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  48. #define SLINK_M_S (1 << 28)
  49. #define SLINK_WAIT (1 << 29)
  50. #define SLINK_GO (1 << 30)
  51. #define SLINK_ENB (1 << 31)
  52. #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  53. #define SLINK_COMMAND2 0x004
  54. #define SLINK_LSBFE (1 << 0)
  55. #define SLINK_SSOE (1 << 1)
  56. #define SLINK_SPIE (1 << 4)
  57. #define SLINK_BIDIROE (1 << 6)
  58. #define SLINK_MODFEN (1 << 7)
  59. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  60. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  61. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  62. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  63. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  64. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  65. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  66. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  67. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  68. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  69. #define SLINK_SPC0 (1 << 29)
  70. #define SLINK_TXEN (1 << 30)
  71. #define SLINK_RXEN (1 << 31)
  72. #define SLINK_STATUS 0x008
  73. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  74. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  75. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  76. #define SLINK_MODF (1 << 16)
  77. #define SLINK_RX_UNF (1 << 18)
  78. #define SLINK_TX_OVF (1 << 19)
  79. #define SLINK_TX_FULL (1 << 20)
  80. #define SLINK_TX_EMPTY (1 << 21)
  81. #define SLINK_RX_FULL (1 << 22)
  82. #define SLINK_RX_EMPTY (1 << 23)
  83. #define SLINK_TX_UNF (1 << 24)
  84. #define SLINK_RX_OVF (1 << 25)
  85. #define SLINK_TX_FLUSH (1 << 26)
  86. #define SLINK_RX_FLUSH (1 << 27)
  87. #define SLINK_SCLK (1 << 28)
  88. #define SLINK_ERR (1 << 29)
  89. #define SLINK_RDY (1 << 30)
  90. #define SLINK_BSY (1 << 31)
  91. #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
  92. SLINK_TX_UNF | SLINK_RX_OVF)
  93. #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  94. #define SLINK_MAS_DATA 0x010
  95. #define SLINK_SLAVE_DATA 0x014
  96. #define SLINK_DMA_CTL 0x018
  97. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  98. #define SLINK_TX_TRIG_1 (0 << 16)
  99. #define SLINK_TX_TRIG_4 (1 << 16)
  100. #define SLINK_TX_TRIG_8 (2 << 16)
  101. #define SLINK_TX_TRIG_16 (3 << 16)
  102. #define SLINK_TX_TRIG_MASK (3 << 16)
  103. #define SLINK_RX_TRIG_1 (0 << 18)
  104. #define SLINK_RX_TRIG_4 (1 << 18)
  105. #define SLINK_RX_TRIG_8 (2 << 18)
  106. #define SLINK_RX_TRIG_16 (3 << 18)
  107. #define SLINK_RX_TRIG_MASK (3 << 18)
  108. #define SLINK_PACKED (1 << 20)
  109. #define SLINK_PACK_SIZE_4 (0 << 21)
  110. #define SLINK_PACK_SIZE_8 (1 << 21)
  111. #define SLINK_PACK_SIZE_16 (2 << 21)
  112. #define SLINK_PACK_SIZE_32 (3 << 21)
  113. #define SLINK_PACK_SIZE_MASK (3 << 21)
  114. #define SLINK_IE_TXC (1 << 26)
  115. #define SLINK_IE_RXC (1 << 27)
  116. #define SLINK_DMA_EN (1 << 31)
  117. #define SLINK_STATUS2 0x01c
  118. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  119. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
  120. #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
  121. #define SLINK_TX_FIFO 0x100
  122. #define SLINK_RX_FIFO 0x180
  123. #define DATA_DIR_TX (1 << 0)
  124. #define DATA_DIR_RX (1 << 1)
  125. #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
  126. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  127. #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  128. #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
  129. #define SLINK_STATUS2_RESET \
  130. (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  131. #define MAX_CHIP_SELECT 4
  132. #define SLINK_FIFO_DEPTH 32
  133. struct tegra_slink_chip_data {
  134. bool cs_hold_time;
  135. };
  136. struct tegra_slink_data {
  137. struct device *dev;
  138. struct spi_master *master;
  139. const struct tegra_slink_chip_data *chip_data;
  140. spinlock_t lock;
  141. struct clk *clk;
  142. struct reset_control *rst;
  143. void __iomem *base;
  144. phys_addr_t phys;
  145. unsigned irq;
  146. u32 cur_speed;
  147. struct spi_device *cur_spi;
  148. unsigned cur_pos;
  149. unsigned cur_len;
  150. unsigned words_per_32bit;
  151. unsigned bytes_per_word;
  152. unsigned curr_dma_words;
  153. unsigned cur_direction;
  154. unsigned cur_rx_pos;
  155. unsigned cur_tx_pos;
  156. unsigned dma_buf_size;
  157. unsigned max_buf_size;
  158. bool is_curr_dma_xfer;
  159. struct completion rx_dma_complete;
  160. struct completion tx_dma_complete;
  161. u32 tx_status;
  162. u32 rx_status;
  163. u32 status_reg;
  164. bool is_packed;
  165. u32 packed_size;
  166. u32 command_reg;
  167. u32 command2_reg;
  168. u32 dma_control_reg;
  169. u32 def_command_reg;
  170. u32 def_command2_reg;
  171. struct completion xfer_completion;
  172. struct spi_transfer *curr_xfer;
  173. struct dma_chan *rx_dma_chan;
  174. u32 *rx_dma_buf;
  175. dma_addr_t rx_dma_phys;
  176. struct dma_async_tx_descriptor *rx_dma_desc;
  177. struct dma_chan *tx_dma_chan;
  178. u32 *tx_dma_buf;
  179. dma_addr_t tx_dma_phys;
  180. struct dma_async_tx_descriptor *tx_dma_desc;
  181. };
  182. static inline u32 tegra_slink_readl(struct tegra_slink_data *tspi,
  183. unsigned long reg)
  184. {
  185. return readl(tspi->base + reg);
  186. }
  187. static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
  188. u32 val, unsigned long reg)
  189. {
  190. writel(val, tspi->base + reg);
  191. /* Read back register to make sure that register writes completed */
  192. if (reg != SLINK_TX_FIFO)
  193. readl(tspi->base + SLINK_MAS_DATA);
  194. }
  195. static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  196. {
  197. u32 val_write;
  198. tegra_slink_readl(tspi, SLINK_STATUS);
  199. /* Write 1 to clear status register */
  200. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  201. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  202. }
  203. static u32 tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  204. struct spi_transfer *t)
  205. {
  206. switch (tspi->bytes_per_word) {
  207. case 0:
  208. return SLINK_PACK_SIZE_4;
  209. case 1:
  210. return SLINK_PACK_SIZE_8;
  211. case 2:
  212. return SLINK_PACK_SIZE_16;
  213. case 4:
  214. return SLINK_PACK_SIZE_32;
  215. default:
  216. return 0;
  217. }
  218. }
  219. static unsigned tegra_slink_calculate_curr_xfer_param(
  220. struct spi_device *spi, struct tegra_slink_data *tspi,
  221. struct spi_transfer *t)
  222. {
  223. unsigned remain_len = t->len - tspi->cur_pos;
  224. unsigned max_word;
  225. unsigned bits_per_word;
  226. unsigned max_len;
  227. unsigned total_fifo_words;
  228. bits_per_word = t->bits_per_word;
  229. tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
  230. if (bits_per_word == 8 || bits_per_word == 16) {
  231. tspi->is_packed = true;
  232. tspi->words_per_32bit = 32/bits_per_word;
  233. } else {
  234. tspi->is_packed = false;
  235. tspi->words_per_32bit = 1;
  236. }
  237. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  238. if (tspi->is_packed) {
  239. max_len = min(remain_len, tspi->max_buf_size);
  240. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  241. total_fifo_words = max_len/4;
  242. } else {
  243. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  244. max_word = min(max_word, tspi->max_buf_size/4);
  245. tspi->curr_dma_words = max_word;
  246. total_fifo_words = max_word;
  247. }
  248. return total_fifo_words;
  249. }
  250. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  251. struct tegra_slink_data *tspi, struct spi_transfer *t)
  252. {
  253. unsigned nbytes;
  254. unsigned tx_empty_count;
  255. u32 fifo_status;
  256. unsigned max_n_32bit;
  257. unsigned i, count;
  258. unsigned int written_words;
  259. unsigned fifo_words_left;
  260. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  261. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  262. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  263. if (tspi->is_packed) {
  264. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  265. written_words = min(fifo_words_left, tspi->curr_dma_words);
  266. nbytes = written_words * tspi->bytes_per_word;
  267. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  268. for (count = 0; count < max_n_32bit; count++) {
  269. u32 x = 0;
  270. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  271. x |= (u32)(*tx_buf++) << (i * 8);
  272. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  273. }
  274. } else {
  275. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  276. written_words = max_n_32bit;
  277. nbytes = written_words * tspi->bytes_per_word;
  278. for (count = 0; count < max_n_32bit; count++) {
  279. u32 x = 0;
  280. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  281. i++, nbytes--)
  282. x |= (u32)(*tx_buf++) << (i * 8);
  283. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  284. }
  285. }
  286. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  287. return written_words;
  288. }
  289. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  290. struct tegra_slink_data *tspi, struct spi_transfer *t)
  291. {
  292. unsigned rx_full_count;
  293. u32 fifo_status;
  294. unsigned i, count;
  295. unsigned int read_words = 0;
  296. unsigned len;
  297. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  298. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  299. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  300. if (tspi->is_packed) {
  301. len = tspi->curr_dma_words * tspi->bytes_per_word;
  302. for (count = 0; count < rx_full_count; count++) {
  303. u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  304. for (i = 0; len && (i < 4); i++, len--)
  305. *rx_buf++ = (x >> i*8) & 0xFF;
  306. }
  307. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  308. read_words += tspi->curr_dma_words;
  309. } else {
  310. for (count = 0; count < rx_full_count; count++) {
  311. u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  312. for (i = 0; (i < tspi->bytes_per_word); i++)
  313. *rx_buf++ = (x >> (i*8)) & 0xFF;
  314. }
  315. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  316. read_words += rx_full_count;
  317. }
  318. return read_words;
  319. }
  320. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  321. struct tegra_slink_data *tspi, struct spi_transfer *t)
  322. {
  323. /* Make the dma buffer to read by cpu */
  324. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  325. tspi->dma_buf_size, DMA_TO_DEVICE);
  326. if (tspi->is_packed) {
  327. unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
  328. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  329. } else {
  330. unsigned int i;
  331. unsigned int count;
  332. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  333. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  334. for (count = 0; count < tspi->curr_dma_words; count++) {
  335. u32 x = 0;
  336. for (i = 0; consume && (i < tspi->bytes_per_word);
  337. i++, consume--)
  338. x |= (u32)(*tx_buf++) << (i * 8);
  339. tspi->tx_dma_buf[count] = x;
  340. }
  341. }
  342. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  343. /* Make the dma buffer to read by dma */
  344. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  345. tspi->dma_buf_size, DMA_TO_DEVICE);
  346. }
  347. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  348. struct tegra_slink_data *tspi, struct spi_transfer *t)
  349. {
  350. unsigned len;
  351. /* Make the dma buffer to read by cpu */
  352. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  353. tspi->dma_buf_size, DMA_FROM_DEVICE);
  354. if (tspi->is_packed) {
  355. len = tspi->curr_dma_words * tspi->bytes_per_word;
  356. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  357. } else {
  358. unsigned int i;
  359. unsigned int count;
  360. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  361. u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
  362. for (count = 0; count < tspi->curr_dma_words; count++) {
  363. u32 x = tspi->rx_dma_buf[count] & rx_mask;
  364. for (i = 0; (i < tspi->bytes_per_word); i++)
  365. *rx_buf++ = (x >> (i*8)) & 0xFF;
  366. }
  367. }
  368. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  369. /* Make the dma buffer to read by dma */
  370. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  371. tspi->dma_buf_size, DMA_FROM_DEVICE);
  372. }
  373. static void tegra_slink_dma_complete(void *args)
  374. {
  375. struct completion *dma_complete = args;
  376. complete(dma_complete);
  377. }
  378. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  379. {
  380. reinit_completion(&tspi->tx_dma_complete);
  381. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  382. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  383. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  384. if (!tspi->tx_dma_desc) {
  385. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  386. return -EIO;
  387. }
  388. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  389. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  390. dmaengine_submit(tspi->tx_dma_desc);
  391. dma_async_issue_pending(tspi->tx_dma_chan);
  392. return 0;
  393. }
  394. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  395. {
  396. reinit_completion(&tspi->rx_dma_complete);
  397. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  398. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  399. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  400. if (!tspi->rx_dma_desc) {
  401. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  402. return -EIO;
  403. }
  404. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  405. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  406. dmaengine_submit(tspi->rx_dma_desc);
  407. dma_async_issue_pending(tspi->rx_dma_chan);
  408. return 0;
  409. }
  410. static int tegra_slink_start_dma_based_transfer(
  411. struct tegra_slink_data *tspi, struct spi_transfer *t)
  412. {
  413. u32 val;
  414. unsigned int len;
  415. int ret = 0;
  416. u32 status;
  417. /* Make sure that Rx and Tx fifo are empty */
  418. status = tegra_slink_readl(tspi, SLINK_STATUS);
  419. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  420. dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x\n",
  421. (unsigned)status);
  422. return -EIO;
  423. }
  424. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  425. val |= tspi->packed_size;
  426. if (tspi->is_packed)
  427. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  428. 4) * 4;
  429. else
  430. len = tspi->curr_dma_words * 4;
  431. /* Set attention level based on length of transfer */
  432. if (len & 0xF)
  433. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  434. else if (((len) >> 4) & 0x1)
  435. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  436. else
  437. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  438. if (tspi->cur_direction & DATA_DIR_TX)
  439. val |= SLINK_IE_TXC;
  440. if (tspi->cur_direction & DATA_DIR_RX)
  441. val |= SLINK_IE_RXC;
  442. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  443. tspi->dma_control_reg = val;
  444. if (tspi->cur_direction & DATA_DIR_TX) {
  445. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  446. wmb();
  447. ret = tegra_slink_start_tx_dma(tspi, len);
  448. if (ret < 0) {
  449. dev_err(tspi->dev,
  450. "Starting tx dma failed, err %d\n", ret);
  451. return ret;
  452. }
  453. /* Wait for tx fifo to be fill before starting slink */
  454. status = tegra_slink_readl(tspi, SLINK_STATUS);
  455. while (!(status & SLINK_TX_FULL))
  456. status = tegra_slink_readl(tspi, SLINK_STATUS);
  457. }
  458. if (tspi->cur_direction & DATA_DIR_RX) {
  459. /* Make the dma buffer to read by dma */
  460. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  461. tspi->dma_buf_size, DMA_FROM_DEVICE);
  462. ret = tegra_slink_start_rx_dma(tspi, len);
  463. if (ret < 0) {
  464. dev_err(tspi->dev,
  465. "Starting rx dma failed, err %d\n", ret);
  466. if (tspi->cur_direction & DATA_DIR_TX)
  467. dmaengine_terminate_all(tspi->tx_dma_chan);
  468. return ret;
  469. }
  470. }
  471. tspi->is_curr_dma_xfer = true;
  472. if (tspi->is_packed) {
  473. val |= SLINK_PACKED;
  474. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  475. /* HW need small delay after settign Packed mode */
  476. udelay(1);
  477. }
  478. tspi->dma_control_reg = val;
  479. val |= SLINK_DMA_EN;
  480. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  481. return ret;
  482. }
  483. static int tegra_slink_start_cpu_based_transfer(
  484. struct tegra_slink_data *tspi, struct spi_transfer *t)
  485. {
  486. u32 val;
  487. unsigned cur_words;
  488. val = tspi->packed_size;
  489. if (tspi->cur_direction & DATA_DIR_TX)
  490. val |= SLINK_IE_TXC;
  491. if (tspi->cur_direction & DATA_DIR_RX)
  492. val |= SLINK_IE_RXC;
  493. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  494. tspi->dma_control_reg = val;
  495. if (tspi->cur_direction & DATA_DIR_TX)
  496. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  497. else
  498. cur_words = tspi->curr_dma_words;
  499. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  500. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  501. tspi->dma_control_reg = val;
  502. tspi->is_curr_dma_xfer = false;
  503. if (tspi->is_packed) {
  504. val |= SLINK_PACKED;
  505. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  506. udelay(1);
  507. wmb();
  508. }
  509. tspi->dma_control_reg = val;
  510. val |= SLINK_DMA_EN;
  511. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  512. return 0;
  513. }
  514. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  515. bool dma_to_memory)
  516. {
  517. struct dma_chan *dma_chan;
  518. u32 *dma_buf;
  519. dma_addr_t dma_phys;
  520. int ret;
  521. struct dma_slave_config dma_sconfig;
  522. dma_chan = dma_request_chan(tspi->dev, dma_to_memory ? "rx" : "tx");
  523. if (IS_ERR(dma_chan))
  524. return dev_err_probe(tspi->dev, PTR_ERR(dma_chan),
  525. "Dma channel is not available\n");
  526. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  527. &dma_phys, GFP_KERNEL);
  528. if (!dma_buf) {
  529. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  530. dma_release_channel(dma_chan);
  531. return -ENOMEM;
  532. }
  533. if (dma_to_memory) {
  534. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  535. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  536. dma_sconfig.src_maxburst = 0;
  537. } else {
  538. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  539. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  540. dma_sconfig.dst_maxburst = 0;
  541. }
  542. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  543. if (ret)
  544. goto scrub;
  545. if (dma_to_memory) {
  546. tspi->rx_dma_chan = dma_chan;
  547. tspi->rx_dma_buf = dma_buf;
  548. tspi->rx_dma_phys = dma_phys;
  549. } else {
  550. tspi->tx_dma_chan = dma_chan;
  551. tspi->tx_dma_buf = dma_buf;
  552. tspi->tx_dma_phys = dma_phys;
  553. }
  554. return 0;
  555. scrub:
  556. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  557. dma_release_channel(dma_chan);
  558. return ret;
  559. }
  560. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  561. bool dma_to_memory)
  562. {
  563. u32 *dma_buf;
  564. dma_addr_t dma_phys;
  565. struct dma_chan *dma_chan;
  566. if (dma_to_memory) {
  567. dma_buf = tspi->rx_dma_buf;
  568. dma_chan = tspi->rx_dma_chan;
  569. dma_phys = tspi->rx_dma_phys;
  570. tspi->rx_dma_chan = NULL;
  571. tspi->rx_dma_buf = NULL;
  572. } else {
  573. dma_buf = tspi->tx_dma_buf;
  574. dma_chan = tspi->tx_dma_chan;
  575. dma_phys = tspi->tx_dma_phys;
  576. tspi->tx_dma_buf = NULL;
  577. tspi->tx_dma_chan = NULL;
  578. }
  579. if (!dma_chan)
  580. return;
  581. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  582. dma_release_channel(dma_chan);
  583. }
  584. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  585. struct spi_transfer *t)
  586. {
  587. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  588. u32 speed;
  589. u8 bits_per_word;
  590. unsigned total_fifo_words;
  591. int ret;
  592. u32 command;
  593. u32 command2;
  594. bits_per_word = t->bits_per_word;
  595. speed = t->speed_hz;
  596. if (speed != tspi->cur_speed) {
  597. dev_pm_opp_set_rate(tspi->dev, speed * 4);
  598. tspi->cur_speed = speed;
  599. }
  600. tspi->cur_spi = spi;
  601. tspi->cur_pos = 0;
  602. tspi->cur_rx_pos = 0;
  603. tspi->cur_tx_pos = 0;
  604. tspi->curr_xfer = t;
  605. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  606. command = tspi->command_reg;
  607. command &= ~SLINK_BIT_LENGTH(~0);
  608. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  609. command2 = tspi->command2_reg;
  610. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  611. tspi->cur_direction = 0;
  612. if (t->rx_buf) {
  613. command2 |= SLINK_RXEN;
  614. tspi->cur_direction |= DATA_DIR_RX;
  615. }
  616. if (t->tx_buf) {
  617. command2 |= SLINK_TXEN;
  618. tspi->cur_direction |= DATA_DIR_TX;
  619. }
  620. /*
  621. * Writing to the command2 register bevore the command register prevents
  622. * a spike in chip_select line 0. This selects the chip_select line
  623. * before changing the chip_select value.
  624. */
  625. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  626. tspi->command2_reg = command2;
  627. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  628. tspi->command_reg = command;
  629. if (total_fifo_words > SLINK_FIFO_DEPTH)
  630. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  631. else
  632. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  633. return ret;
  634. }
  635. static int tegra_slink_setup(struct spi_device *spi)
  636. {
  637. static const u32 cs_pol_bit[MAX_CHIP_SELECT] = {
  638. SLINK_CS_POLARITY,
  639. SLINK_CS_POLARITY1,
  640. SLINK_CS_POLARITY2,
  641. SLINK_CS_POLARITY3,
  642. };
  643. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  644. u32 val;
  645. unsigned long flags;
  646. int ret;
  647. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  648. spi->bits_per_word,
  649. spi->mode & SPI_CPOL ? "" : "~",
  650. spi->mode & SPI_CPHA ? "" : "~",
  651. spi->max_speed_hz);
  652. ret = pm_runtime_resume_and_get(tspi->dev);
  653. if (ret < 0) {
  654. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  655. return ret;
  656. }
  657. spin_lock_irqsave(&tspi->lock, flags);
  658. val = tspi->def_command_reg;
  659. if (spi->mode & SPI_CS_HIGH)
  660. val |= cs_pol_bit[spi->chip_select];
  661. else
  662. val &= ~cs_pol_bit[spi->chip_select];
  663. tspi->def_command_reg = val;
  664. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  665. spin_unlock_irqrestore(&tspi->lock, flags);
  666. pm_runtime_put(tspi->dev);
  667. return 0;
  668. }
  669. static int tegra_slink_prepare_message(struct spi_master *master,
  670. struct spi_message *msg)
  671. {
  672. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  673. struct spi_device *spi = msg->spi;
  674. tegra_slink_clear_status(tspi);
  675. tspi->command_reg = tspi->def_command_reg;
  676. tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
  677. tspi->command2_reg = tspi->def_command2_reg;
  678. tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
  679. tspi->command_reg &= ~SLINK_MODES;
  680. if (spi->mode & SPI_CPHA)
  681. tspi->command_reg |= SLINK_CK_SDA;
  682. if (spi->mode & SPI_CPOL)
  683. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  684. else
  685. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
  686. return 0;
  687. }
  688. static int tegra_slink_transfer_one(struct spi_master *master,
  689. struct spi_device *spi,
  690. struct spi_transfer *xfer)
  691. {
  692. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  693. int ret;
  694. reinit_completion(&tspi->xfer_completion);
  695. ret = tegra_slink_start_transfer_one(spi, xfer);
  696. if (ret < 0) {
  697. dev_err(tspi->dev,
  698. "spi can not start transfer, err %d\n", ret);
  699. return ret;
  700. }
  701. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  702. SLINK_DMA_TIMEOUT);
  703. if (WARN_ON(ret == 0)) {
  704. dev_err(tspi->dev,
  705. "spi transfer timeout, err %d\n", ret);
  706. return -EIO;
  707. }
  708. if (tspi->tx_status)
  709. return tspi->tx_status;
  710. if (tspi->rx_status)
  711. return tspi->rx_status;
  712. return 0;
  713. }
  714. static int tegra_slink_unprepare_message(struct spi_master *master,
  715. struct spi_message *msg)
  716. {
  717. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  718. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  719. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  720. return 0;
  721. }
  722. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  723. {
  724. struct spi_transfer *t = tspi->curr_xfer;
  725. unsigned long flags;
  726. spin_lock_irqsave(&tspi->lock, flags);
  727. if (tspi->tx_status || tspi->rx_status ||
  728. (tspi->status_reg & SLINK_BSY)) {
  729. dev_err(tspi->dev,
  730. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  731. dev_err(tspi->dev,
  732. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  733. tspi->command2_reg, tspi->dma_control_reg);
  734. reset_control_assert(tspi->rst);
  735. udelay(2);
  736. reset_control_deassert(tspi->rst);
  737. complete(&tspi->xfer_completion);
  738. goto exit;
  739. }
  740. if (tspi->cur_direction & DATA_DIR_RX)
  741. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  742. if (tspi->cur_direction & DATA_DIR_TX)
  743. tspi->cur_pos = tspi->cur_tx_pos;
  744. else
  745. tspi->cur_pos = tspi->cur_rx_pos;
  746. if (tspi->cur_pos == t->len) {
  747. complete(&tspi->xfer_completion);
  748. goto exit;
  749. }
  750. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  751. tegra_slink_start_cpu_based_transfer(tspi, t);
  752. exit:
  753. spin_unlock_irqrestore(&tspi->lock, flags);
  754. return IRQ_HANDLED;
  755. }
  756. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  757. {
  758. struct spi_transfer *t = tspi->curr_xfer;
  759. long wait_status;
  760. int err = 0;
  761. unsigned total_fifo_words;
  762. unsigned long flags;
  763. /* Abort dmas if any error */
  764. if (tspi->cur_direction & DATA_DIR_TX) {
  765. if (tspi->tx_status) {
  766. dmaengine_terminate_all(tspi->tx_dma_chan);
  767. err += 1;
  768. } else {
  769. wait_status = wait_for_completion_interruptible_timeout(
  770. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  771. if (wait_status <= 0) {
  772. dmaengine_terminate_all(tspi->tx_dma_chan);
  773. dev_err(tspi->dev, "TxDma Xfer failed\n");
  774. err += 1;
  775. }
  776. }
  777. }
  778. if (tspi->cur_direction & DATA_DIR_RX) {
  779. if (tspi->rx_status) {
  780. dmaengine_terminate_all(tspi->rx_dma_chan);
  781. err += 2;
  782. } else {
  783. wait_status = wait_for_completion_interruptible_timeout(
  784. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  785. if (wait_status <= 0) {
  786. dmaengine_terminate_all(tspi->rx_dma_chan);
  787. dev_err(tspi->dev, "RxDma Xfer failed\n");
  788. err += 2;
  789. }
  790. }
  791. }
  792. spin_lock_irqsave(&tspi->lock, flags);
  793. if (err) {
  794. dev_err(tspi->dev,
  795. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  796. dev_err(tspi->dev,
  797. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  798. tspi->command2_reg, tspi->dma_control_reg);
  799. reset_control_assert(tspi->rst);
  800. udelay(2);
  801. reset_control_assert(tspi->rst);
  802. complete(&tspi->xfer_completion);
  803. spin_unlock_irqrestore(&tspi->lock, flags);
  804. return IRQ_HANDLED;
  805. }
  806. if (tspi->cur_direction & DATA_DIR_RX)
  807. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  808. if (tspi->cur_direction & DATA_DIR_TX)
  809. tspi->cur_pos = tspi->cur_tx_pos;
  810. else
  811. tspi->cur_pos = tspi->cur_rx_pos;
  812. if (tspi->cur_pos == t->len) {
  813. complete(&tspi->xfer_completion);
  814. goto exit;
  815. }
  816. /* Continue transfer in current message */
  817. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  818. tspi, t);
  819. if (total_fifo_words > SLINK_FIFO_DEPTH)
  820. err = tegra_slink_start_dma_based_transfer(tspi, t);
  821. else
  822. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  823. exit:
  824. spin_unlock_irqrestore(&tspi->lock, flags);
  825. return IRQ_HANDLED;
  826. }
  827. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  828. {
  829. struct tegra_slink_data *tspi = context_data;
  830. if (!tspi->is_curr_dma_xfer)
  831. return handle_cpu_based_xfer(tspi);
  832. return handle_dma_based_xfer(tspi);
  833. }
  834. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  835. {
  836. struct tegra_slink_data *tspi = context_data;
  837. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  838. if (tspi->cur_direction & DATA_DIR_TX)
  839. tspi->tx_status = tspi->status_reg &
  840. (SLINK_TX_OVF | SLINK_TX_UNF);
  841. if (tspi->cur_direction & DATA_DIR_RX)
  842. tspi->rx_status = tspi->status_reg &
  843. (SLINK_RX_OVF | SLINK_RX_UNF);
  844. tegra_slink_clear_status(tspi);
  845. return IRQ_WAKE_THREAD;
  846. }
  847. static const struct tegra_slink_chip_data tegra30_spi_cdata = {
  848. .cs_hold_time = true,
  849. };
  850. static const struct tegra_slink_chip_data tegra20_spi_cdata = {
  851. .cs_hold_time = false,
  852. };
  853. static const struct of_device_id tegra_slink_of_match[] = {
  854. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  855. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  856. {}
  857. };
  858. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  859. static int tegra_slink_probe(struct platform_device *pdev)
  860. {
  861. struct spi_master *master;
  862. struct tegra_slink_data *tspi;
  863. struct resource *r;
  864. int ret, spi_irq;
  865. const struct tegra_slink_chip_data *cdata = NULL;
  866. cdata = of_device_get_match_data(&pdev->dev);
  867. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  868. if (!master) {
  869. dev_err(&pdev->dev, "master allocation failed\n");
  870. return -ENOMEM;
  871. }
  872. /* the spi->mode bits understood by this driver: */
  873. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  874. master->setup = tegra_slink_setup;
  875. master->prepare_message = tegra_slink_prepare_message;
  876. master->transfer_one = tegra_slink_transfer_one;
  877. master->unprepare_message = tegra_slink_unprepare_message;
  878. master->auto_runtime_pm = true;
  879. master->num_chipselect = MAX_CHIP_SELECT;
  880. platform_set_drvdata(pdev, master);
  881. tspi = spi_master_get_devdata(master);
  882. tspi->master = master;
  883. tspi->dev = &pdev->dev;
  884. tspi->chip_data = cdata;
  885. spin_lock_init(&tspi->lock);
  886. if (of_property_read_u32(tspi->dev->of_node, "spi-max-frequency",
  887. &master->max_speed_hz))
  888. master->max_speed_hz = 25000000; /* 25MHz */
  889. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  890. if (!r) {
  891. dev_err(&pdev->dev, "No IO memory resource\n");
  892. ret = -ENODEV;
  893. goto exit_free_master;
  894. }
  895. tspi->phys = r->start;
  896. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  897. if (IS_ERR(tspi->base)) {
  898. ret = PTR_ERR(tspi->base);
  899. goto exit_free_master;
  900. }
  901. /* disabled clock may cause interrupt storm upon request */
  902. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  903. if (IS_ERR(tspi->clk)) {
  904. ret = PTR_ERR(tspi->clk);
  905. dev_err(&pdev->dev, "Can not get clock %d\n", ret);
  906. goto exit_free_master;
  907. }
  908. tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
  909. if (IS_ERR(tspi->rst)) {
  910. dev_err(&pdev->dev, "can not get reset\n");
  911. ret = PTR_ERR(tspi->rst);
  912. goto exit_free_master;
  913. }
  914. ret = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
  915. if (ret)
  916. goto exit_free_master;
  917. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  918. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  919. ret = tegra_slink_init_dma_param(tspi, true);
  920. if (ret < 0)
  921. goto exit_free_master;
  922. ret = tegra_slink_init_dma_param(tspi, false);
  923. if (ret < 0)
  924. goto exit_rx_dma_free;
  925. tspi->max_buf_size = tspi->dma_buf_size;
  926. init_completion(&tspi->tx_dma_complete);
  927. init_completion(&tspi->rx_dma_complete);
  928. init_completion(&tspi->xfer_completion);
  929. pm_runtime_enable(&pdev->dev);
  930. ret = pm_runtime_resume_and_get(&pdev->dev);
  931. if (ret) {
  932. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  933. goto exit_pm_disable;
  934. }
  935. reset_control_assert(tspi->rst);
  936. udelay(2);
  937. reset_control_deassert(tspi->rst);
  938. spi_irq = platform_get_irq(pdev, 0);
  939. if (spi_irq < 0)
  940. return spi_irq;
  941. tspi->irq = spi_irq;
  942. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  943. tegra_slink_isr_thread, IRQF_ONESHOT,
  944. dev_name(&pdev->dev), tspi);
  945. if (ret < 0) {
  946. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  947. tspi->irq);
  948. goto exit_pm_put;
  949. }
  950. tspi->def_command_reg = SLINK_M_S;
  951. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  952. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  953. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  954. master->dev.of_node = pdev->dev.of_node;
  955. ret = spi_register_master(master);
  956. if (ret < 0) {
  957. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  958. goto exit_free_irq;
  959. }
  960. pm_runtime_put(&pdev->dev);
  961. return ret;
  962. exit_free_irq:
  963. free_irq(spi_irq, tspi);
  964. exit_pm_put:
  965. pm_runtime_put(&pdev->dev);
  966. exit_pm_disable:
  967. pm_runtime_force_suspend(&pdev->dev);
  968. tegra_slink_deinit_dma_param(tspi, false);
  969. exit_rx_dma_free:
  970. tegra_slink_deinit_dma_param(tspi, true);
  971. exit_free_master:
  972. spi_master_put(master);
  973. return ret;
  974. }
  975. static int tegra_slink_remove(struct platform_device *pdev)
  976. {
  977. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  978. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  979. spi_unregister_master(master);
  980. free_irq(tspi->irq, tspi);
  981. pm_runtime_force_suspend(&pdev->dev);
  982. if (tspi->tx_dma_chan)
  983. tegra_slink_deinit_dma_param(tspi, false);
  984. if (tspi->rx_dma_chan)
  985. tegra_slink_deinit_dma_param(tspi, true);
  986. spi_master_put(master);
  987. return 0;
  988. }
  989. #ifdef CONFIG_PM_SLEEP
  990. static int tegra_slink_suspend(struct device *dev)
  991. {
  992. struct spi_master *master = dev_get_drvdata(dev);
  993. return spi_master_suspend(master);
  994. }
  995. static int tegra_slink_resume(struct device *dev)
  996. {
  997. struct spi_master *master = dev_get_drvdata(dev);
  998. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  999. int ret;
  1000. ret = pm_runtime_resume_and_get(dev);
  1001. if (ret < 0) {
  1002. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1003. return ret;
  1004. }
  1005. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1006. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1007. pm_runtime_put(dev);
  1008. return spi_master_resume(master);
  1009. }
  1010. #endif
  1011. static int __maybe_unused tegra_slink_runtime_suspend(struct device *dev)
  1012. {
  1013. struct spi_master *master = dev_get_drvdata(dev);
  1014. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1015. /* Flush all write which are in PPSB queue by reading back */
  1016. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1017. clk_disable_unprepare(tspi->clk);
  1018. return 0;
  1019. }
  1020. static int __maybe_unused tegra_slink_runtime_resume(struct device *dev)
  1021. {
  1022. struct spi_master *master = dev_get_drvdata(dev);
  1023. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1024. int ret;
  1025. ret = clk_prepare_enable(tspi->clk);
  1026. if (ret < 0) {
  1027. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1028. return ret;
  1029. }
  1030. return 0;
  1031. }
  1032. static const struct dev_pm_ops slink_pm_ops = {
  1033. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1034. tegra_slink_runtime_resume, NULL)
  1035. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1036. };
  1037. static struct platform_driver tegra_slink_driver = {
  1038. .driver = {
  1039. .name = "spi-tegra-slink",
  1040. .pm = &slink_pm_ops,
  1041. .of_match_table = tegra_slink_of_match,
  1042. },
  1043. .probe = tegra_slink_probe,
  1044. .remove = tegra_slink_remove,
  1045. };
  1046. module_platform_driver(tegra_slink_driver);
  1047. MODULE_ALIAS("platform:spi-tegra-slink");
  1048. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1049. MODULE_AUTHOR("Laxman Dewangan <[email protected]>");
  1050. MODULE_LICENSE("GPL v2");