spi-tegra20-slink.c 34 KB

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