blob: 4b32fd7d0570cedcee595bfa28ff24099a152eae [file] [log] [blame]
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001/*
2 * SPI bus driver for the Topcliff PCH used by Intel SoCs
Grant Likely65308c42010-09-29 17:31:34 +09003 *
Tomoya MORINAGA2b246282011-10-28 09:35:22 +09004 * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 */
19
Grant Likely65308c42010-09-29 17:31:34 +090020#include <linux/delay.h>
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060021#include <linux/pci.h>
22#include <linux/wait.h>
23#include <linux/spi/spi.h>
24#include <linux/interrupt.h>
25#include <linux/sched.h>
26#include <linux/spi/spidev.h>
27#include <linux/module.h>
28#include <linux/device.h>
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +090029#include <linux/platform_device.h>
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060030
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +090031#include <linux/dmaengine.h>
32#include <linux/pch_dma.h>
33
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060034/* Register offsets */
35#define PCH_SPCR 0x00 /* SPI control register */
36#define PCH_SPBRR 0x04 /* SPI baud rate register */
37#define PCH_SPSR 0x08 /* SPI status register */
38#define PCH_SPDWR 0x0C /* SPI write data register */
39#define PCH_SPDRR 0x10 /* SPI read data register */
40#define PCH_SSNXCR 0x18 /* SSN Expand Control Register */
41#define PCH_SRST 0x1C /* SPI reset register */
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +090042#define PCH_ADDRESS_SIZE 0x20
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060043
44#define PCH_SPSR_TFD 0x000007C0
45#define PCH_SPSR_RFD 0x0000F800
46
47#define PCH_READABLE(x) (((x) & PCH_SPSR_RFD)>>11)
48#define PCH_WRITABLE(x) (((x) & PCH_SPSR_TFD)>>6)
49
50#define PCH_RX_THOLD 7
51#define PCH_RX_THOLD_MAX 15
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060052
Tomoya MORINAGAf3e03e2e2011-09-06 17:16:38 +090053#define PCH_TX_THOLD 2
54
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060055#define PCH_MAX_BAUDRATE 5000000
56#define PCH_MAX_FIFO_DEPTH 16
57
58#define STATUS_RUNNING 1
59#define STATUS_EXITING 2
60#define PCH_SLEEP_TIME 10
61
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060062#define SSN_LOW 0x02U
Tomoya MORINAGA8b7aa962011-09-06 17:16:35 +090063#define SSN_HIGH 0x03U
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060064#define SSN_NO_CONTROL 0x00U
65#define PCH_MAX_CS 0xFF
66#define PCI_DEVICE_ID_GE_SPI 0x8816
67
68#define SPCR_SPE_BIT (1 << 0)
69#define SPCR_MSTR_BIT (1 << 1)
70#define SPCR_LSBF_BIT (1 << 4)
71#define SPCR_CPHA_BIT (1 << 5)
72#define SPCR_CPOL_BIT (1 << 6)
73#define SPCR_TFIE_BIT (1 << 8)
74#define SPCR_RFIE_BIT (1 << 9)
75#define SPCR_FIE_BIT (1 << 10)
76#define SPCR_ORIE_BIT (1 << 11)
77#define SPCR_MDFIE_BIT (1 << 12)
78#define SPCR_FICLR_BIT (1 << 24)
79#define SPSR_TFI_BIT (1 << 0)
80#define SPSR_RFI_BIT (1 << 1)
81#define SPSR_FI_BIT (1 << 2)
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +090082#define SPSR_ORF_BIT (1 << 3)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060083#define SPBRR_SIZE_BIT (1 << 10)
84
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +090085#define PCH_ALL (SPCR_TFIE_BIT|SPCR_RFIE_BIT|SPCR_FIE_BIT|\
86 SPCR_ORIE_BIT|SPCR_MDFIE_BIT)
Grant Likely65308c42010-09-29 17:31:34 +090087
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060088#define SPCR_RFIC_FIELD 20
89#define SPCR_TFIC_FIELD 16
90
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +090091#define MASK_SPBRR_SPBR_BITS ((1 << 10) - 1)
92#define MASK_RFIC_SPCR_BITS (0xf << SPCR_RFIC_FIELD)
93#define MASK_TFIC_SPCR_BITS (0xf << SPCR_TFIC_FIELD)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -060094
95#define PCH_CLOCK_HZ 50000000
96#define PCH_MAX_SPBR 1023
97
Tomoya MORINAGA2b246282011-10-28 09:35:22 +090098/* Definition for ML7213/ML7223/ML7831 by LAPIS Semiconductor */
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +090099#define PCI_VENDOR_ID_ROHM 0x10DB
100#define PCI_DEVICE_ID_ML7213_SPI 0x802c
Tomoya MORINAGA2e2de2e32011-06-17 09:34:25 +0900101#define PCI_DEVICE_ID_ML7223_SPI 0x800F
Tomoya MORINAGA92b3a5c2011-10-28 09:35:21 +0900102#define PCI_DEVICE_ID_ML7831_SPI 0x8816
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900103
104/*
105 * Set the number of SPI instance max
106 * Intel EG20T PCH : 1ch
Tomoya MORINAGA2b246282011-10-28 09:35:22 +0900107 * LAPIS Semiconductor ML7213 IOH : 2ch
108 * LAPIS Semiconductor ML7223 IOH : 1ch
109 * LAPIS Semiconductor ML7831 IOH : 1ch
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900110*/
111#define PCH_SPI_MAX_DEV 2
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600112
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900113#define PCH_BUF_SIZE 4096
114#define PCH_DMA_TRANS_SIZE 12
115
116static int use_dma = 1;
117
118struct pch_spi_dma_ctrl {
119 struct dma_async_tx_descriptor *desc_tx;
120 struct dma_async_tx_descriptor *desc_rx;
121 struct pch_dma_slave param_tx;
122 struct pch_dma_slave param_rx;
123 struct dma_chan *chan_tx;
124 struct dma_chan *chan_rx;
125 struct scatterlist *sg_tx_p;
126 struct scatterlist *sg_rx_p;
127 struct scatterlist sg_tx;
128 struct scatterlist sg_rx;
129 int nent;
130 void *tx_buf_virt;
131 void *rx_buf_virt;
132 dma_addr_t tx_buf_dma;
133 dma_addr_t rx_buf_dma;
134};
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600135/**
136 * struct pch_spi_data - Holds the SPI channel specific details
137 * @io_remap_addr: The remapped PCI base address
138 * @master: Pointer to the SPI master structure
139 * @work: Reference to work queue handler
140 * @wk: Workqueue for carrying out execution of the
141 * requests
142 * @wait: Wait queue for waking up upon receiving an
143 * interrupt.
144 * @transfer_complete: Status of SPI Transfer
145 * @bcurrent_msg_processing: Status flag for message processing
146 * @lock: Lock for protecting this structure
147 * @queue: SPI Message queue
148 * @status: Status of the SPI driver
149 * @bpw_len: Length of data to be transferred in bits per
150 * word
151 * @transfer_active: Flag showing active transfer
152 * @tx_index: Transmit data count; for bookkeeping during
153 * transfer
154 * @rx_index: Receive data count; for bookkeeping during
155 * transfer
156 * @tx_buff: Buffer for data to be transmitted
157 * @rx_index: Buffer for Received data
158 * @n_curnt_chip: The chip number that this SPI driver currently
159 * operates on
160 * @current_chip: Reference to the current chip that this SPI
161 * driver currently operates on
162 * @current_msg: The current message that this SPI driver is
163 * handling
164 * @cur_trans: The current transfer that this SPI driver is
165 * handling
166 * @board_dat: Reference to the SPI device data structure
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900167 * @plat_dev: platform_device structure
168 * @ch: SPI channel number
169 * @irq_reg_sts: Status of IRQ registration
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600170 */
171struct pch_spi_data {
172 void __iomem *io_remap_addr;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900173 unsigned long io_base_addr;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600174 struct spi_master *master;
175 struct work_struct work;
176 struct workqueue_struct *wk;
177 wait_queue_head_t wait;
178 u8 transfer_complete;
179 u8 bcurrent_msg_processing;
180 spinlock_t lock;
181 struct list_head queue;
182 u8 status;
183 u32 bpw_len;
184 u8 transfer_active;
185 u32 tx_index;
186 u32 rx_index;
187 u16 *pkt_tx_buff;
188 u16 *pkt_rx_buff;
189 u8 n_curnt_chip;
190 struct spi_device *current_chip;
191 struct spi_message *current_msg;
192 struct spi_transfer *cur_trans;
193 struct pch_spi_board_data *board_dat;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900194 struct platform_device *plat_dev;
195 int ch;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900196 struct pch_spi_dma_ctrl dma;
197 int use_dma;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900198 u8 irq_reg_sts;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600199};
200
201/**
202 * struct pch_spi_board_data - Holds the SPI device specific details
203 * @pdev: Pointer to the PCI device
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600204 * @suspend_sts: Status of suspend
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900205 * @num: The number of SPI device instance
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600206 */
207struct pch_spi_board_data {
208 struct pci_dev *pdev;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600209 u8 suspend_sts;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900210 int num;
211};
212
213struct pch_pd_dev_save {
214 int num;
215 struct platform_device *pd_save[PCH_SPI_MAX_DEV];
216 struct pch_spi_board_data *board_dat;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600217};
218
Axel Line290cf22011-12-15 08:11:25 +0800219static DEFINE_PCI_DEVICE_TABLE(pch_spi_pcidev_id) = {
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900220 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_GE_SPI), 1, },
221 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_SPI), 2, },
Tomoya MORINAGA2e2de2e32011-06-17 09:34:25 +0900222 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_SPI), 1, },
Tomoya MORINAGA92b3a5c2011-10-28 09:35:21 +0900223 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_SPI), 1, },
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900224 { }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600225};
226
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600227/**
228 * pch_spi_writereg() - Performs register writes
229 * @master: Pointer to struct spi_master.
230 * @idx: Register offset.
231 * @val: Value to be written to register.
232 */
233static inline void pch_spi_writereg(struct spi_master *master, int idx, u32 val)
234{
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600235 struct pch_spi_data *data = spi_master_get_devdata(master);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600236 iowrite32(val, (data->io_remap_addr + idx));
237}
238
239/**
240 * pch_spi_readreg() - Performs register reads
241 * @master: Pointer to struct spi_master.
242 * @idx: Register offset.
243 */
244static inline u32 pch_spi_readreg(struct spi_master *master, int idx)
245{
246 struct pch_spi_data *data = spi_master_get_devdata(master);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600247 return ioread32(data->io_remap_addr + idx);
248}
249
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600250static inline void pch_spi_setclr_reg(struct spi_master *master, int idx,
251 u32 set, u32 clr)
252{
253 u32 tmp = pch_spi_readreg(master, idx);
254 tmp = (tmp & ~clr) | set;
255 pch_spi_writereg(master, idx, tmp);
256}
257
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600258static void pch_spi_set_master_mode(struct spi_master *master)
259{
260 pch_spi_setclr_reg(master, PCH_SPCR, SPCR_MSTR_BIT, 0);
261}
262
263/**
264 * pch_spi_clear_fifo() - Clears the Transmit and Receive FIFOs
265 * @master: Pointer to struct spi_master.
266 */
267static void pch_spi_clear_fifo(struct spi_master *master)
268{
269 pch_spi_setclr_reg(master, PCH_SPCR, SPCR_FICLR_BIT, 0);
270 pch_spi_setclr_reg(master, PCH_SPCR, 0, SPCR_FICLR_BIT);
271}
272
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600273static void pch_spi_handler_sub(struct pch_spi_data *data, u32 reg_spsr_val,
274 void __iomem *io_remap_addr)
275{
276 u32 n_read, tx_index, rx_index, bpw_len;
277 u16 *pkt_rx_buffer, *pkt_tx_buff;
278 int read_cnt;
279 u32 reg_spcr_val;
280 void __iomem *spsr;
281 void __iomem *spdrr;
282 void __iomem *spdwr;
283
284 spsr = io_remap_addr + PCH_SPSR;
285 iowrite32(reg_spsr_val, spsr);
286
287 if (data->transfer_active) {
288 rx_index = data->rx_index;
289 tx_index = data->tx_index;
290 bpw_len = data->bpw_len;
291 pkt_rx_buffer = data->pkt_rx_buff;
292 pkt_tx_buff = data->pkt_tx_buff;
293
294 spdrr = io_remap_addr + PCH_SPDRR;
295 spdwr = io_remap_addr + PCH_SPDWR;
296
297 n_read = PCH_READABLE(reg_spsr_val);
298
299 for (read_cnt = 0; (read_cnt < n_read); read_cnt++) {
300 pkt_rx_buffer[rx_index++] = ioread32(spdrr);
301 if (tx_index < bpw_len)
302 iowrite32(pkt_tx_buff[tx_index++], spdwr);
303 }
304
305 /* disable RFI if not needed */
306 if ((bpw_len - rx_index) <= PCH_MAX_FIFO_DEPTH) {
307 reg_spcr_val = ioread32(io_remap_addr + PCH_SPCR);
Grant Likely65308c42010-09-29 17:31:34 +0900308 reg_spcr_val &= ~SPCR_RFIE_BIT; /* disable RFI */
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600309
310 /* reset rx threshold */
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900311 reg_spcr_val &= ~MASK_RFIC_SPCR_BITS;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600312 reg_spcr_val |= (PCH_RX_THOLD_MAX << SPCR_RFIC_FIELD);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900313
314 iowrite32(reg_spcr_val, (io_remap_addr + PCH_SPCR));
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600315 }
316
317 /* update counts */
318 data->tx_index = tx_index;
319 data->rx_index = rx_index;
320
Danny Kukawkade3bd7e2012-02-14 15:35:03 +0100321 /* if transfer complete interrupt */
322 if (reg_spsr_val & SPSR_FI_BIT) {
323 if ((tx_index == bpw_len) && (rx_index == tx_index)) {
324 /* disable interrupts */
325 pch_spi_setclr_reg(data->master, PCH_SPCR, 0,
326 PCH_ALL);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600327
Danny Kukawkade3bd7e2012-02-14 15:35:03 +0100328 /* transfer is completed;
329 inform pch_spi_process_messages */
330 data->transfer_complete = true;
331 data->transfer_active = false;
332 wake_up(&data->wait);
333 } else {
334 dev_err(&data->master->dev,
335 "%s : Transfer is not completed",
336 __func__);
337 }
Tomoya MORINAGA373b0eb2011-09-06 17:16:36 +0900338 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600339 }
340}
341
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600342/**
343 * pch_spi_handler() - Interrupt handler
344 * @irq: The interrupt number.
345 * @dev_id: Pointer to struct pch_spi_board_data.
346 */
347static irqreturn_t pch_spi_handler(int irq, void *dev_id)
348{
349 u32 reg_spsr_val;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600350 void __iomem *spsr;
351 void __iomem *io_remap_addr;
352 irqreturn_t ret = IRQ_NONE;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +0900353 struct pch_spi_data *data = dev_id;
354 struct pch_spi_board_data *board_dat = data->board_dat;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600355
356 if (board_dat->suspend_sts) {
357 dev_dbg(&board_dat->pdev->dev,
358 "%s returning due to suspend\n", __func__);
359 return IRQ_NONE;
360 }
361
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600362 io_remap_addr = data->io_remap_addr;
363 spsr = io_remap_addr + PCH_SPSR;
364
365 reg_spsr_val = ioread32(spsr);
366
Tomoya MORINAGA25e803f2011-09-06 17:16:37 +0900367 if (reg_spsr_val & SPSR_ORF_BIT) {
368 dev_err(&board_dat->pdev->dev, "%s Over run error\n", __func__);
369 if (data->current_msg->complete != 0) {
370 data->transfer_complete = true;
371 data->current_msg->status = -EIO;
372 data->current_msg->complete(data->current_msg->context);
373 data->bcurrent_msg_processing = false;
374 data->current_msg = NULL;
375 data->cur_trans = NULL;
376 }
377 }
378
379 if (data->use_dma)
380 return IRQ_NONE;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900381
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600382 /* Check if the interrupt is for SPI device */
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600383 if (reg_spsr_val & (SPSR_FI_BIT | SPSR_RFI_BIT)) {
384 pch_spi_handler_sub(data, reg_spsr_val, io_remap_addr);
385 ret = IRQ_HANDLED;
386 }
387
388 dev_dbg(&board_dat->pdev->dev, "%s EXIT return value=%d\n",
389 __func__, ret);
390
391 return ret;
392}
393
394/**
395 * pch_spi_set_baud_rate() - Sets SPBR field in SPBRR
396 * @master: Pointer to struct spi_master.
397 * @speed_hz: Baud rate.
398 */
399static void pch_spi_set_baud_rate(struct spi_master *master, u32 speed_hz)
400{
Grant Likely65308c42010-09-29 17:31:34 +0900401 u32 n_spbr = PCH_CLOCK_HZ / (speed_hz * 2);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600402
403 /* if baud rate is less than we can support limit it */
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600404 if (n_spbr > PCH_MAX_SPBR)
405 n_spbr = PCH_MAX_SPBR;
406
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900407 pch_spi_setclr_reg(master, PCH_SPBRR, n_spbr, MASK_SPBRR_SPBR_BITS);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600408}
409
410/**
411 * pch_spi_set_bits_per_word() - Sets SIZE field in SPBRR
412 * @master: Pointer to struct spi_master.
413 * @bits_per_word: Bits per word for SPI transfer.
414 */
415static void pch_spi_set_bits_per_word(struct spi_master *master,
416 u8 bits_per_word)
417{
418 if (bits_per_word == 8)
419 pch_spi_setclr_reg(master, PCH_SPBRR, 0, SPBRR_SIZE_BIT);
420 else
421 pch_spi_setclr_reg(master, PCH_SPBRR, SPBRR_SIZE_BIT, 0);
422}
423
424/**
425 * pch_spi_setup_transfer() - Configures the PCH SPI hardware for transfer
426 * @spi: Pointer to struct spi_device.
427 */
428static void pch_spi_setup_transfer(struct spi_device *spi)
429{
Grant Likely65308c42010-09-29 17:31:34 +0900430 u32 flags = 0;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600431
432 dev_dbg(&spi->dev, "%s SPBRR content =%x setting baud rate=%d\n",
433 __func__, pch_spi_readreg(spi->master, PCH_SPBRR),
434 spi->max_speed_hz);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600435 pch_spi_set_baud_rate(spi->master, spi->max_speed_hz);
436
437 /* set bits per word */
438 pch_spi_set_bits_per_word(spi->master, spi->bits_per_word);
439
Grant Likely65308c42010-09-29 17:31:34 +0900440 if (!(spi->mode & SPI_LSB_FIRST))
441 flags |= SPCR_LSBF_BIT;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600442 if (spi->mode & SPI_CPOL)
Grant Likely65308c42010-09-29 17:31:34 +0900443 flags |= SPCR_CPOL_BIT;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600444 if (spi->mode & SPI_CPHA)
Grant Likely65308c42010-09-29 17:31:34 +0900445 flags |= SPCR_CPHA_BIT;
446 pch_spi_setclr_reg(spi->master, PCH_SPCR, flags,
447 (SPCR_LSBF_BIT | SPCR_CPOL_BIT | SPCR_CPHA_BIT));
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600448
449 /* Clear the FIFO by toggling FICLR to 1 and back to 0 */
450 pch_spi_clear_fifo(spi->master);
451}
452
453/**
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600454 * pch_spi_reset() - Clears SPI registers
455 * @master: Pointer to struct spi_master.
456 */
457static void pch_spi_reset(struct spi_master *master)
458{
459 /* write 1 to reset SPI */
460 pch_spi_writereg(master, PCH_SRST, 0x1);
461
462 /* clear reset */
463 pch_spi_writereg(master, PCH_SRST, 0x0);
464}
465
466static int pch_spi_setup(struct spi_device *pspi)
467{
468 /* check bits per word */
Grant Likely65308c42010-09-29 17:31:34 +0900469 if (pspi->bits_per_word == 0) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600470 pspi->bits_per_word = 8;
471 dev_dbg(&pspi->dev, "%s 8 bits per word\n", __func__);
472 }
473
Grant Likely65308c42010-09-29 17:31:34 +0900474 if ((pspi->bits_per_word != 8) && (pspi->bits_per_word != 16)) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600475 dev_err(&pspi->dev, "%s Invalid bits per word\n", __func__);
476 return -EINVAL;
477 }
478
479 /* Check baud rate setting */
480 /* if baud rate of chip is greater than
481 max we can support,return error */
482 if ((pspi->max_speed_hz) > PCH_MAX_BAUDRATE)
483 pspi->max_speed_hz = PCH_MAX_BAUDRATE;
484
485 dev_dbg(&pspi->dev, "%s MODE = %x\n", __func__,
Grant Likely65308c42010-09-29 17:31:34 +0900486 (pspi->mode) & (SPI_CPOL | SPI_CPHA));
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600487
488 return 0;
489}
490
491static int pch_spi_transfer(struct spi_device *pspi, struct spi_message *pmsg)
492{
493
494 struct spi_transfer *transfer;
495 struct pch_spi_data *data = spi_master_get_devdata(pspi->master);
496 int retval;
497 unsigned long flags;
498
499 /* validate spi message and baud rate */
Grant Likely65308c42010-09-29 17:31:34 +0900500 if (unlikely(list_empty(&pmsg->transfers) == 1)) {
501 dev_err(&pspi->dev, "%s list empty\n", __func__);
502 retval = -EINVAL;
503 goto err_out;
504 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600505
Grant Likely65308c42010-09-29 17:31:34 +0900506 if (unlikely(pspi->max_speed_hz == 0)) {
507 dev_err(&pspi->dev, "%s pch_spi_tranfer maxspeed=%d\n",
508 __func__, pspi->max_speed_hz);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600509 retval = -EINVAL;
510 goto err_out;
511 }
512
513 dev_dbg(&pspi->dev, "%s Transfer List not empty. "
514 "Transfer Speed is set.\n", __func__);
515
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900516 spin_lock_irqsave(&data->lock, flags);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600517 /* validate Tx/Rx buffers and Transfer length */
518 list_for_each_entry(transfer, &pmsg->transfers, transfer_list) {
Grant Likely65308c42010-09-29 17:31:34 +0900519 if (!transfer->tx_buf && !transfer->rx_buf) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600520 dev_err(&pspi->dev,
521 "%s Tx and Rx buffer NULL\n", __func__);
522 retval = -EINVAL;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900523 goto err_return_spinlock;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600524 }
525
Grant Likely65308c42010-09-29 17:31:34 +0900526 if (!transfer->len) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600527 dev_err(&pspi->dev, "%s Transfer length invalid\n",
528 __func__);
529 retval = -EINVAL;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900530 goto err_return_spinlock;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600531 }
532
533 dev_dbg(&pspi->dev, "%s Tx/Rx buffer valid. Transfer length"
534 " valid\n", __func__);
535
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900536 /* if baud rate has been specified validate the same */
Grant Likely65308c42010-09-29 17:31:34 +0900537 if (transfer->speed_hz > PCH_MAX_BAUDRATE)
538 transfer->speed_hz = PCH_MAX_BAUDRATE;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600539
540 /* if bits per word has been specified validate the same */
541 if (transfer->bits_per_word) {
542 if ((transfer->bits_per_word != 8)
543 && (transfer->bits_per_word != 16)) {
544 retval = -EINVAL;
545 dev_err(&pspi->dev,
546 "%s Invalid bits per word\n", __func__);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900547 goto err_return_spinlock;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600548 }
549 }
550 }
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900551 spin_unlock_irqrestore(&data->lock, flags);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600552
Grant Likely65308c42010-09-29 17:31:34 +0900553 /* We won't process any messages if we have been asked to terminate */
554 if (data->status == STATUS_EXITING) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600555 dev_err(&pspi->dev, "%s status = STATUS_EXITING.\n", __func__);
556 retval = -ESHUTDOWN;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900557 goto err_out;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600558 }
559
560 /* If suspended ,return -EINVAL */
561 if (data->board_dat->suspend_sts) {
Grant Likely65308c42010-09-29 17:31:34 +0900562 dev_err(&pspi->dev, "%s suspend; returning EINVAL\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600563 retval = -EINVAL;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900564 goto err_out;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600565 }
566
567 /* set status of message */
568 pmsg->actual_length = 0;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600569 dev_dbg(&pspi->dev, "%s - pmsg->status =%d\n", __func__, pmsg->status);
570
571 pmsg->status = -EINPROGRESS;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900572 spin_lock_irqsave(&data->lock, flags);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600573 /* add message to queue */
574 list_add_tail(&pmsg->queue, &data->queue);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900575 spin_unlock_irqrestore(&data->lock, flags);
576
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600577 dev_dbg(&pspi->dev, "%s - Invoked list_add_tail\n", __func__);
578
579 /* schedule work queue to run */
580 queue_work(data->wk, &data->work);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600581 dev_dbg(&pspi->dev, "%s - Invoked queue work\n", __func__);
582
583 retval = 0;
584
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600585err_out:
586 dev_dbg(&pspi->dev, "%s RETURN=%d\n", __func__, retval);
587 return retval;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900588err_return_spinlock:
589 dev_dbg(&pspi->dev, "%s RETURN=%d\n", __func__, retval);
590 spin_unlock_irqrestore(&data->lock, flags);
591 return retval;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600592}
593
594static inline void pch_spi_select_chip(struct pch_spi_data *data,
595 struct spi_device *pspi)
596{
Grant Likely65308c42010-09-29 17:31:34 +0900597 if (data->current_chip != NULL) {
598 if (pspi->chip_select != data->n_curnt_chip) {
599 dev_dbg(&pspi->dev, "%s : different slave\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600600 data->current_chip = NULL;
601 }
602 }
603
604 data->current_chip = pspi;
605
606 data->n_curnt_chip = data->current_chip->chip_select;
607
608 dev_dbg(&pspi->dev, "%s :Invoking pch_spi_setup_transfer\n", __func__);
609 pch_spi_setup_transfer(pspi);
610}
611
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900612static void pch_spi_set_tx(struct pch_spi_data *data, int *bpw)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600613{
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600614 int size;
615 u32 n_writes;
616 int j;
617 struct spi_message *pmsg;
618 const u8 *tx_buf;
619 const u16 *tx_sbuf;
620
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600621 /* set baud rate if needed */
622 if (data->cur_trans->speed_hz) {
Grant Likely65308c42010-09-29 17:31:34 +0900623 dev_dbg(&data->master->dev, "%s:setting baud rate\n", __func__);
624 pch_spi_set_baud_rate(data->master, data->cur_trans->speed_hz);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600625 }
626
627 /* set bits per word if needed */
Grant Likely65308c42010-09-29 17:31:34 +0900628 if (data->cur_trans->bits_per_word &&
629 (data->current_msg->spi->bits_per_word != data->cur_trans->bits_per_word)) {
630 dev_dbg(&data->master->dev, "%s:set bits per word\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600631 pch_spi_set_bits_per_word(data->master,
Grant Likely65308c42010-09-29 17:31:34 +0900632 data->cur_trans->bits_per_word);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600633 *bpw = data->cur_trans->bits_per_word;
634 } else {
635 *bpw = data->current_msg->spi->bits_per_word;
636 }
637
638 /* reset Tx/Rx index */
639 data->tx_index = 0;
640 data->rx_index = 0;
641
642 data->bpw_len = data->cur_trans->len / (*bpw / 8);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600643
644 /* find alloc size */
Grant Likely65308c42010-09-29 17:31:34 +0900645 size = data->cur_trans->len * sizeof(*data->pkt_tx_buff);
646
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600647 /* allocate memory for pkt_tx_buff & pkt_rx_buffer */
648 data->pkt_tx_buff = kzalloc(size, GFP_KERNEL);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600649 if (data->pkt_tx_buff != NULL) {
650 data->pkt_rx_buff = kzalloc(size, GFP_KERNEL);
Grant Likely65308c42010-09-29 17:31:34 +0900651 if (!data->pkt_rx_buff)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600652 kfree(data->pkt_tx_buff);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600653 }
654
Grant Likely65308c42010-09-29 17:31:34 +0900655 if (!data->pkt_rx_buff) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600656 /* flush queue and set status of all transfers to -ENOMEM */
Grant Likely65308c42010-09-29 17:31:34 +0900657 dev_err(&data->master->dev, "%s :kzalloc failed\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600658 list_for_each_entry(pmsg, data->queue.next, queue) {
659 pmsg->status = -ENOMEM;
660
661 if (pmsg->complete != 0)
662 pmsg->complete(pmsg->context);
663
664 /* delete from queue */
665 list_del_init(&pmsg->queue);
666 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600667 return;
668 }
669
670 /* copy Tx Data */
Grant Likely65308c42010-09-29 17:31:34 +0900671 if (data->cur_trans->tx_buf != NULL) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600672 if (*bpw == 8) {
Grant Likely65308c42010-09-29 17:31:34 +0900673 tx_buf = data->cur_trans->tx_buf;
674 for (j = 0; j < data->bpw_len; j++)
675 data->pkt_tx_buff[j] = *tx_buf++;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600676 } else {
Grant Likely65308c42010-09-29 17:31:34 +0900677 tx_sbuf = data->cur_trans->tx_buf;
678 for (j = 0; j < data->bpw_len; j++)
679 data->pkt_tx_buff[j] = *tx_sbuf++;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600680 }
681 }
682
683 /* if len greater than PCH_MAX_FIFO_DEPTH, write 16,else len bytes */
Grant Likely65308c42010-09-29 17:31:34 +0900684 n_writes = data->bpw_len;
685 if (n_writes > PCH_MAX_FIFO_DEPTH)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600686 n_writes = PCH_MAX_FIFO_DEPTH;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600687
Grant Likely65308c42010-09-29 17:31:34 +0900688 dev_dbg(&data->master->dev, "\n%s:Pulling down SSN low - writing "
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600689 "0x2 to SSNXCR\n", __func__);
690 pch_spi_writereg(data->master, PCH_SSNXCR, SSN_LOW);
691
Grant Likely65308c42010-09-29 17:31:34 +0900692 for (j = 0; j < n_writes; j++)
693 pch_spi_writereg(data->master, PCH_SPDWR, data->pkt_tx_buff[j]);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600694
695 /* update tx_index */
696 data->tx_index = j;
697
698 /* reset transfer complete flag */
699 data->transfer_complete = false;
700 data->transfer_active = true;
701}
702
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900703static void pch_spi_nomore_transfer(struct pch_spi_data *data)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600704{
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900705 struct spi_message *pmsg;
Grant Likely65308c42010-09-29 17:31:34 +0900706 dev_dbg(&data->master->dev, "%s called\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600707 /* Invoke complete callback
Grant Likely65308c42010-09-29 17:31:34 +0900708 * [To the spi core..indicating end of transfer] */
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600709 data->current_msg->status = 0;
710
Grant Likely65308c42010-09-29 17:31:34 +0900711 if (data->current_msg->complete != 0) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600712 dev_dbg(&data->master->dev,
713 "%s:Invoking callback of SPI core\n", __func__);
714 data->current_msg->complete(data->current_msg->context);
715 }
716
717 /* update status in global variable */
718 data->bcurrent_msg_processing = false;
719
720 dev_dbg(&data->master->dev,
721 "%s:data->bcurrent_msg_processing = false\n", __func__);
722
723 data->current_msg = NULL;
724 data->cur_trans = NULL;
725
Grant Likely65308c42010-09-29 17:31:34 +0900726 /* check if we have items in list and not suspending
727 * return 1 if list empty */
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600728 if ((list_empty(&data->queue) == 0) &&
Grant Likely65308c42010-09-29 17:31:34 +0900729 (!data->board_dat->suspend_sts) &&
730 (data->status != STATUS_EXITING)) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600731 /* We have some more work to do (either there is more tranint
Grant Likely65308c42010-09-29 17:31:34 +0900732 * bpw;sfer requests in the current message or there are
733 *more messages)
734 */
735 dev_dbg(&data->master->dev, "%s:Invoke queue_work\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600736 queue_work(data->wk, &data->work);
Grant Likely65308c42010-09-29 17:31:34 +0900737 } else if (data->board_dat->suspend_sts ||
738 data->status == STATUS_EXITING) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600739 dev_dbg(&data->master->dev,
740 "%s suspend/remove initiated, flushing queue\n",
741 __func__);
742 list_for_each_entry(pmsg, data->queue.next, queue) {
743 pmsg->status = -EIO;
744
Grant Likely65308c42010-09-29 17:31:34 +0900745 if (pmsg->complete)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600746 pmsg->complete(pmsg->context);
747
748 /* delete from queue */
749 list_del_init(&pmsg->queue);
750 }
751 }
752}
753
754static void pch_spi_set_ir(struct pch_spi_data *data)
755{
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900756 /* enable interrupts, set threshold, enable SPI */
757 if ((data->bpw_len) > PCH_MAX_FIFO_DEPTH)
Justin P. Mattock77e58ef2010-12-31 09:50:31 -0800758 /* set receive threshold to PCH_RX_THOLD */
Grant Likely65308c42010-09-29 17:31:34 +0900759 pch_spi_setclr_reg(data->master, PCH_SPCR,
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900760 PCH_RX_THOLD << SPCR_RFIC_FIELD |
761 SPCR_FIE_BIT | SPCR_RFIE_BIT |
762 SPCR_ORIE_BIT | SPCR_SPE_BIT,
763 MASK_RFIC_SPCR_BITS | PCH_ALL);
764 else
Justin P. Mattock77e58ef2010-12-31 09:50:31 -0800765 /* set receive threshold to maximum */
Grant Likely65308c42010-09-29 17:31:34 +0900766 pch_spi_setclr_reg(data->master, PCH_SPCR,
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900767 PCH_RX_THOLD_MAX << SPCR_RFIC_FIELD |
768 SPCR_FIE_BIT | SPCR_ORIE_BIT |
769 SPCR_SPE_BIT,
770 MASK_RFIC_SPCR_BITS | PCH_ALL);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600771
772 /* Wait until the transfer completes; go to sleep after
773 initiating the transfer. */
774 dev_dbg(&data->master->dev,
775 "%s:waiting for transfer to get over\n", __func__);
776
777 wait_event_interruptible(data->wait, data->transfer_complete);
778
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600779 /* clear all interrupts */
780 pch_spi_writereg(data->master, PCH_SPSR,
Grant Likely65308c42010-09-29 17:31:34 +0900781 pch_spi_readreg(data->master, PCH_SPSR));
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900782 /* Disable interrupts and SPI transfer */
783 pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL | SPCR_SPE_BIT);
784 /* clear FIFO */
785 pch_spi_clear_fifo(data->master);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600786}
787
788static void pch_spi_copy_rx_data(struct pch_spi_data *data, int bpw)
789{
790 int j;
791 u8 *rx_buf;
792 u16 *rx_sbuf;
793
794 /* copy Rx Data */
Grant Likely65308c42010-09-29 17:31:34 +0900795 if (!data->cur_trans->rx_buf)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600796 return;
797
798 if (bpw == 8) {
Grant Likely65308c42010-09-29 17:31:34 +0900799 rx_buf = data->cur_trans->rx_buf;
800 for (j = 0; j < data->bpw_len; j++)
801 *rx_buf++ = data->pkt_rx_buff[j] & 0xFF;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600802 } else {
Grant Likely65308c42010-09-29 17:31:34 +0900803 rx_sbuf = data->cur_trans->rx_buf;
804 for (j = 0; j < data->bpw_len; j++)
805 *rx_sbuf++ = data->pkt_rx_buff[j];
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -0600806 }
807}
808
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900809static void pch_spi_copy_rx_data_for_dma(struct pch_spi_data *data, int bpw)
810{
811 int j;
812 u8 *rx_buf;
813 u16 *rx_sbuf;
814 const u8 *rx_dma_buf;
815 const u16 *rx_dma_sbuf;
816
817 /* copy Rx Data */
818 if (!data->cur_trans->rx_buf)
819 return;
820
821 if (bpw == 8) {
822 rx_buf = data->cur_trans->rx_buf;
823 rx_dma_buf = data->dma.rx_buf_virt;
824 for (j = 0; j < data->bpw_len; j++)
825 *rx_buf++ = *rx_dma_buf++ & 0xFF;
826 } else {
827 rx_sbuf = data->cur_trans->rx_buf;
828 rx_dma_sbuf = data->dma.rx_buf_virt;
829 for (j = 0; j < data->bpw_len; j++)
830 *rx_sbuf++ = *rx_dma_sbuf++;
831 }
832}
833
Tomoya MORINAGA25e803f2011-09-06 17:16:37 +0900834static int pch_spi_start_transfer(struct pch_spi_data *data)
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900835{
836 struct pch_spi_dma_ctrl *dma;
837 unsigned long flags;
Tomoya MORINAGA25e803f2011-09-06 17:16:37 +0900838 int rtn;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900839
840 dma = &data->dma;
841
842 spin_lock_irqsave(&data->lock, flags);
843
844 /* disable interrupts, SPI set enable */
845 pch_spi_setclr_reg(data->master, PCH_SPCR, SPCR_SPE_BIT, PCH_ALL);
846
847 spin_unlock_irqrestore(&data->lock, flags);
848
849 /* Wait until the transfer completes; go to sleep after
850 initiating the transfer. */
851 dev_dbg(&data->master->dev,
852 "%s:waiting for transfer to get over\n", __func__);
Tomoya MORINAGA25e803f2011-09-06 17:16:37 +0900853 rtn = wait_event_interruptible_timeout(data->wait,
854 data->transfer_complete,
855 msecs_to_jiffies(2 * HZ));
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900856
857 dma_sync_sg_for_cpu(&data->master->dev, dma->sg_rx_p, dma->nent,
858 DMA_FROM_DEVICE);
Tomoya MORINAGA27504be2011-09-06 17:16:34 +0900859
860 dma_sync_sg_for_cpu(&data->master->dev, dma->sg_tx_p, dma->nent,
861 DMA_FROM_DEVICE);
862 memset(data->dma.tx_buf_virt, 0, PAGE_SIZE);
863
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900864 async_tx_ack(dma->desc_rx);
865 async_tx_ack(dma->desc_tx);
866 kfree(dma->sg_tx_p);
867 kfree(dma->sg_rx_p);
868
869 spin_lock_irqsave(&data->lock, flags);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900870
871 /* clear fifo threshold, disable interrupts, disable SPI transfer */
872 pch_spi_setclr_reg(data->master, PCH_SPCR, 0,
873 MASK_RFIC_SPCR_BITS | MASK_TFIC_SPCR_BITS | PCH_ALL |
874 SPCR_SPE_BIT);
875 /* clear all interrupts */
876 pch_spi_writereg(data->master, PCH_SPSR,
877 pch_spi_readreg(data->master, PCH_SPSR));
878 /* clear FIFO */
879 pch_spi_clear_fifo(data->master);
880
881 spin_unlock_irqrestore(&data->lock, flags);
Tomoya MORINAGA25e803f2011-09-06 17:16:37 +0900882
883 return rtn;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900884}
885
886static void pch_dma_rx_complete(void *arg)
887{
888 struct pch_spi_data *data = arg;
889
890 /* transfer is completed;inform pch_spi_process_messages_dma */
891 data->transfer_complete = true;
892 wake_up_interruptible(&data->wait);
893}
894
895static bool pch_spi_filter(struct dma_chan *chan, void *slave)
896{
897 struct pch_dma_slave *param = slave;
898
899 if ((chan->chan_id == param->chan_id) &&
900 (param->dma_dev == chan->device->dev)) {
901 chan->private = param;
902 return true;
903 } else {
904 return false;
905 }
906}
907
908static void pch_spi_request_dma(struct pch_spi_data *data, int bpw)
909{
910 dma_cap_mask_t mask;
911 struct dma_chan *chan;
912 struct pci_dev *dma_dev;
913 struct pch_dma_slave *param;
914 struct pch_spi_dma_ctrl *dma;
915 unsigned int width;
916
917 if (bpw == 8)
918 width = PCH_DMA_WIDTH_1_BYTE;
919 else
920 width = PCH_DMA_WIDTH_2_BYTES;
921
922 dma = &data->dma;
923 dma_cap_zero(mask);
924 dma_cap_set(DMA_SLAVE, mask);
925
926 /* Get DMA's dev information */
Tomoya MORINAGAee2ece52011-12-09 13:11:42 +0900927 dma_dev = pci_get_bus_and_slot(data->board_dat->pdev->bus->number,
928 PCI_DEVFN(12, 0));
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +0900929
930 /* Set Tx DMA */
931 param = &dma->param_tx;
932 param->dma_dev = &dma_dev->dev;
933 param->chan_id = data->master->bus_num * 2; /* Tx = 0, 2 */
934 param->tx_reg = data->io_base_addr + PCH_SPDWR;
935 param->width = width;
936 chan = dma_request_channel(mask, pch_spi_filter, param);
937 if (!chan) {
938 dev_err(&data->master->dev,
939 "ERROR: dma_request_channel FAILS(Tx)\n");
940 data->use_dma = 0;
941 return;
942 }
943 dma->chan_tx = chan;
944
945 /* Set Rx DMA */
946 param = &dma->param_rx;
947 param->dma_dev = &dma_dev->dev;
948 param->chan_id = data->master->bus_num * 2 + 1; /* Rx = Tx + 1 */
949 param->rx_reg = data->io_base_addr + PCH_SPDRR;
950 param->width = width;
951 chan = dma_request_channel(mask, pch_spi_filter, param);
952 if (!chan) {
953 dev_err(&data->master->dev,
954 "ERROR: dma_request_channel FAILS(Rx)\n");
955 dma_release_channel(dma->chan_tx);
956 dma->chan_tx = NULL;
957 data->use_dma = 0;
958 return;
959 }
960 dma->chan_rx = chan;
961}
962
963static void pch_spi_release_dma(struct pch_spi_data *data)
964{
965 struct pch_spi_dma_ctrl *dma;
966
967 dma = &data->dma;
968 if (dma->chan_tx) {
969 dma_release_channel(dma->chan_tx);
970 dma->chan_tx = NULL;
971 }
972 if (dma->chan_rx) {
973 dma_release_channel(dma->chan_rx);
974 dma->chan_rx = NULL;
975 }
976 return;
977}
978
979static void pch_spi_handle_dma(struct pch_spi_data *data, int *bpw)
980{
981 const u8 *tx_buf;
982 const u16 *tx_sbuf;
983 u8 *tx_dma_buf;
984 u16 *tx_dma_sbuf;
985 struct scatterlist *sg;
986 struct dma_async_tx_descriptor *desc_tx;
987 struct dma_async_tx_descriptor *desc_rx;
988 int num;
989 int i;
990 int size;
991 int rem;
992 unsigned long flags;
993 struct pch_spi_dma_ctrl *dma;
994
995 dma = &data->dma;
996
997 /* set baud rate if needed */
998 if (data->cur_trans->speed_hz) {
999 dev_dbg(&data->master->dev, "%s:setting baud rate\n", __func__);
1000 spin_lock_irqsave(&data->lock, flags);
1001 pch_spi_set_baud_rate(data->master, data->cur_trans->speed_hz);
1002 spin_unlock_irqrestore(&data->lock, flags);
1003 }
1004
1005 /* set bits per word if needed */
1006 if (data->cur_trans->bits_per_word &&
1007 (data->current_msg->spi->bits_per_word !=
1008 data->cur_trans->bits_per_word)) {
1009 dev_dbg(&data->master->dev, "%s:set bits per word\n", __func__);
1010 spin_lock_irqsave(&data->lock, flags);
1011 pch_spi_set_bits_per_word(data->master,
1012 data->cur_trans->bits_per_word);
1013 spin_unlock_irqrestore(&data->lock, flags);
1014 *bpw = data->cur_trans->bits_per_word;
1015 } else {
1016 *bpw = data->current_msg->spi->bits_per_word;
1017 }
1018 data->bpw_len = data->cur_trans->len / (*bpw / 8);
1019
1020 /* copy Tx Data */
1021 if (data->cur_trans->tx_buf != NULL) {
1022 if (*bpw == 8) {
1023 tx_buf = data->cur_trans->tx_buf;
1024 tx_dma_buf = dma->tx_buf_virt;
1025 for (i = 0; i < data->bpw_len; i++)
1026 *tx_dma_buf++ = *tx_buf++;
1027 } else {
1028 tx_sbuf = data->cur_trans->tx_buf;
1029 tx_dma_sbuf = dma->tx_buf_virt;
1030 for (i = 0; i < data->bpw_len; i++)
1031 *tx_dma_sbuf++ = *tx_sbuf++;
1032 }
1033 }
1034 if (data->bpw_len > PCH_DMA_TRANS_SIZE) {
1035 num = data->bpw_len / PCH_DMA_TRANS_SIZE + 1;
1036 size = PCH_DMA_TRANS_SIZE;
1037 rem = data->bpw_len % PCH_DMA_TRANS_SIZE;
1038 } else {
1039 num = 1;
1040 size = data->bpw_len;
1041 rem = data->bpw_len;
1042 }
1043 dev_dbg(&data->master->dev, "%s num=%d size=%d rem=%d\n",
1044 __func__, num, size, rem);
1045 spin_lock_irqsave(&data->lock, flags);
1046
1047 /* set receive fifo threshold and transmit fifo threshold */
1048 pch_spi_setclr_reg(data->master, PCH_SPCR,
1049 ((size - 1) << SPCR_RFIC_FIELD) |
Tomoya MORINAGAf3e03e2e2011-09-06 17:16:38 +09001050 (PCH_TX_THOLD << SPCR_TFIC_FIELD),
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001051 MASK_RFIC_SPCR_BITS | MASK_TFIC_SPCR_BITS);
1052
1053 spin_unlock_irqrestore(&data->lock, flags);
1054
1055 /* RX */
1056 dma->sg_rx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
1057 sg_init_table(dma->sg_rx_p, num); /* Initialize SG table */
1058 /* offset, length setting */
1059 sg = dma->sg_rx_p;
1060 for (i = 0; i < num; i++, sg++) {
Tomoya MORINAGAf3e03e2e2011-09-06 17:16:38 +09001061 if (i == (num - 2)) {
1062 sg->offset = size * i;
1063 sg->offset = sg->offset * (*bpw / 8);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001064 sg_set_page(sg, virt_to_page(dma->rx_buf_virt), rem,
1065 sg->offset);
1066 sg_dma_len(sg) = rem;
Tomoya MORINAGAf3e03e2e2011-09-06 17:16:38 +09001067 } else if (i == (num - 1)) {
1068 sg->offset = size * (i - 1) + rem;
1069 sg->offset = sg->offset * (*bpw / 8);
1070 sg_set_page(sg, virt_to_page(dma->rx_buf_virt), size,
1071 sg->offset);
1072 sg_dma_len(sg) = size;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001073 } else {
Tomoya MORINAGAf3e03e2e2011-09-06 17:16:38 +09001074 sg->offset = size * i;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001075 sg->offset = sg->offset * (*bpw / 8);
1076 sg_set_page(sg, virt_to_page(dma->rx_buf_virt), size,
1077 sg->offset);
1078 sg_dma_len(sg) = size;
1079 }
1080 sg_dma_address(sg) = dma->rx_buf_dma + sg->offset;
1081 }
1082 sg = dma->sg_rx_p;
1083 desc_rx = dma->chan_rx->device->device_prep_slave_sg(dma->chan_rx, sg,
Vinod Koula485df42011-10-14 10:47:38 +05301084 num, DMA_DEV_TO_MEM,
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001085 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1086 if (!desc_rx) {
1087 dev_err(&data->master->dev, "%s:device_prep_slave_sg Failed\n",
1088 __func__);
1089 return;
1090 }
1091 dma_sync_sg_for_device(&data->master->dev, sg, num, DMA_FROM_DEVICE);
1092 desc_rx->callback = pch_dma_rx_complete;
1093 desc_rx->callback_param = data;
1094 dma->nent = num;
1095 dma->desc_rx = desc_rx;
1096
1097 /* TX */
Tomoya MORINAGAf3e03e2e2011-09-06 17:16:38 +09001098 if (data->bpw_len > PCH_DMA_TRANS_SIZE) {
1099 num = data->bpw_len / PCH_DMA_TRANS_SIZE;
1100 size = PCH_DMA_TRANS_SIZE;
1101 rem = 16;
1102 } else {
1103 num = 1;
1104 size = data->bpw_len;
1105 rem = data->bpw_len;
1106 }
1107
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001108 dma->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
1109 sg_init_table(dma->sg_tx_p, num); /* Initialize SG table */
1110 /* offset, length setting */
1111 sg = dma->sg_tx_p;
1112 for (i = 0; i < num; i++, sg++) {
1113 if (i == 0) {
1114 sg->offset = 0;
1115 sg_set_page(sg, virt_to_page(dma->tx_buf_virt), rem,
1116 sg->offset);
1117 sg_dma_len(sg) = rem;
1118 } else {
1119 sg->offset = rem + size * (i - 1);
1120 sg->offset = sg->offset * (*bpw / 8);
1121 sg_set_page(sg, virt_to_page(dma->tx_buf_virt), size,
1122 sg->offset);
1123 sg_dma_len(sg) = size;
1124 }
1125 sg_dma_address(sg) = dma->tx_buf_dma + sg->offset;
1126 }
1127 sg = dma->sg_tx_p;
1128 desc_tx = dma->chan_tx->device->device_prep_slave_sg(dma->chan_tx,
Vinod Koula485df42011-10-14 10:47:38 +05301129 sg, num, DMA_MEM_TO_DEV,
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001130 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1131 if (!desc_tx) {
1132 dev_err(&data->master->dev, "%s:device_prep_slave_sg Failed\n",
1133 __func__);
1134 return;
1135 }
1136 dma_sync_sg_for_device(&data->master->dev, sg, num, DMA_TO_DEVICE);
1137 desc_tx->callback = NULL;
1138 desc_tx->callback_param = data;
1139 dma->nent = num;
1140 dma->desc_tx = desc_tx;
1141
1142 dev_dbg(&data->master->dev, "\n%s:Pulling down SSN low - writing "
1143 "0x2 to SSNXCR\n", __func__);
1144
1145 spin_lock_irqsave(&data->lock, flags);
1146 pch_spi_writereg(data->master, PCH_SSNXCR, SSN_LOW);
1147 desc_rx->tx_submit(desc_rx);
1148 desc_tx->tx_submit(desc_tx);
1149 spin_unlock_irqrestore(&data->lock, flags);
1150
1151 /* reset transfer complete flag */
1152 data->transfer_complete = false;
1153}
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001154
1155static void pch_spi_process_messages(struct work_struct *pwork)
1156{
1157 struct spi_message *pmsg;
Grant Likely65308c42010-09-29 17:31:34 +09001158 struct pch_spi_data *data;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001159 int bpw;
1160
Grant Likely65308c42010-09-29 17:31:34 +09001161 data = container_of(pwork, struct pch_spi_data, work);
Grant Likely8e41b522010-10-13 23:03:15 -06001162 dev_dbg(&data->master->dev, "%s data initialized\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001163
1164 spin_lock(&data->lock);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001165 /* check if suspend has been initiated;if yes flush queue */
Grant Likely65308c42010-09-29 17:31:34 +09001166 if (data->board_dat->suspend_sts || (data->status == STATUS_EXITING)) {
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001167 dev_dbg(&data->master->dev, "%s suspend/remove initiated,"
1168 "flushing queue\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001169 list_for_each_entry(pmsg, data->queue.next, queue) {
1170 pmsg->status = -EIO;
1171
1172 if (pmsg->complete != 0) {
1173 spin_unlock(&data->lock);
1174 pmsg->complete(pmsg->context);
1175 spin_lock(&data->lock);
1176 }
1177
1178 /* delete from queue */
1179 list_del_init(&pmsg->queue);
1180 }
1181
1182 spin_unlock(&data->lock);
1183 return;
1184 }
1185
1186 data->bcurrent_msg_processing = true;
1187 dev_dbg(&data->master->dev,
1188 "%s Set data->bcurrent_msg_processing= true\n", __func__);
1189
1190 /* Get the message from the queue and delete it from there. */
Grant Likely65308c42010-09-29 17:31:34 +09001191 data->current_msg = list_entry(data->queue.next, struct spi_message,
1192 queue);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001193
1194 list_del_init(&data->current_msg->queue);
1195
1196 data->current_msg->status = 0;
1197
1198 pch_spi_select_chip(data, data->current_msg->spi);
1199
1200 spin_unlock(&data->lock);
1201
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001202 if (data->use_dma)
1203 pch_spi_request_dma(data,
1204 data->current_msg->spi->bits_per_word);
Tomoya MORINAGA8b7aa962011-09-06 17:16:35 +09001205 pch_spi_writereg(data->master, PCH_SSNXCR, SSN_NO_CONTROL);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001206 do {
1207 /* If we are already processing a message get the next
1208 transfer structure from the message otherwise retrieve
1209 the 1st transfer request from the message. */
1210 spin_lock(&data->lock);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001211 if (data->cur_trans == NULL) {
1212 data->cur_trans =
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001213 list_entry(data->current_msg->transfers.next,
1214 struct spi_transfer, transfer_list);
1215 dev_dbg(&data->master->dev, "%s "
1216 ":Getting 1st transfer message\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001217 } else {
1218 data->cur_trans =
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001219 list_entry(data->cur_trans->transfer_list.next,
1220 struct spi_transfer, transfer_list);
1221 dev_dbg(&data->master->dev, "%s "
1222 ":Getting next transfer message\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001223 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001224 spin_unlock(&data->lock);
1225
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001226 if (data->use_dma) {
1227 pch_spi_handle_dma(data, &bpw);
Tomoya MORINAGA25e803f2011-09-06 17:16:37 +09001228 if (!pch_spi_start_transfer(data))
1229 goto out;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001230 pch_spi_copy_rx_data_for_dma(data, bpw);
1231 } else {
1232 pch_spi_set_tx(data, &bpw);
1233 pch_spi_set_ir(data);
1234 pch_spi_copy_rx_data(data, bpw);
1235 kfree(data->pkt_rx_buff);
1236 data->pkt_rx_buff = NULL;
1237 kfree(data->pkt_tx_buff);
1238 data->pkt_tx_buff = NULL;
1239 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001240 /* increment message count */
1241 data->current_msg->actual_length += data->cur_trans->len;
1242
1243 dev_dbg(&data->master->dev,
1244 "%s:data->current_msg->actual_length=%d\n",
1245 __func__, data->current_msg->actual_length);
1246
1247 /* check for delay */
1248 if (data->cur_trans->delay_usecs) {
1249 dev_dbg(&data->master->dev, "%s:"
1250 "delay in usec=%d\n", __func__,
1251 data->cur_trans->delay_usecs);
1252 udelay(data->cur_trans->delay_usecs);
1253 }
1254
1255 spin_lock(&data->lock);
1256
1257 /* No more transfer in this message. */
1258 if ((data->cur_trans->transfer_list.next) ==
1259 &(data->current_msg->transfers)) {
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001260 pch_spi_nomore_transfer(data);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001261 }
1262
1263 spin_unlock(&data->lock);
1264
Grant Likely65308c42010-09-29 17:31:34 +09001265 } while (data->cur_trans != NULL);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001266
Tomoya MORINAGA25e803f2011-09-06 17:16:37 +09001267out:
Tomoya MORINAGA8b7aa962011-09-06 17:16:35 +09001268 pch_spi_writereg(data->master, PCH_SSNXCR, SSN_HIGH);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001269 if (data->use_dma)
1270 pch_spi_release_dma(data);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001271}
1272
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001273static void pch_spi_free_resources(struct pch_spi_board_data *board_dat,
1274 struct pch_spi_data *data)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001275{
1276 dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);
1277
1278 /* free workqueue */
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001279 if (data->wk != NULL) {
1280 destroy_workqueue(data->wk);
1281 data->wk = NULL;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001282 dev_dbg(&board_dat->pdev->dev,
1283 "%s destroy_workqueue invoked successfully\n",
1284 __func__);
1285 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001286}
1287
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001288static int pch_spi_get_resources(struct pch_spi_board_data *board_dat,
1289 struct pch_spi_data *data)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001290{
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001291 int retval = 0;
1292
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001293 dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);
1294
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001295 /* create workqueue */
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001296 data->wk = create_singlethread_workqueue(KBUILD_MODNAME);
1297 if (!data->wk) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001298 dev_err(&board_dat->pdev->dev,
1299 "%s create_singlet hread_workqueue failed\n", __func__);
1300 retval = -EBUSY;
1301 goto err_return;
1302 }
1303
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001304 /* reset PCH SPI h/w */
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001305 pch_spi_reset(data->master);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001306 dev_dbg(&board_dat->pdev->dev,
1307 "%s pch_spi_reset invoked successfully\n", __func__);
1308
Grant Likely65308c42010-09-29 17:31:34 +09001309 dev_dbg(&board_dat->pdev->dev, "%s data->irq_reg_sts=true\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001310
1311err_return:
1312 if (retval != 0) {
1313 dev_err(&board_dat->pdev->dev,
1314 "%s FAIL:invoking pch_spi_free_resources\n", __func__);
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001315 pch_spi_free_resources(board_dat, data);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001316 }
1317
1318 dev_dbg(&board_dat->pdev->dev, "%s Return=%d\n", __func__, retval);
1319
1320 return retval;
1321}
1322
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001323static void pch_free_dma_buf(struct pch_spi_board_data *board_dat,
1324 struct pch_spi_data *data)
1325{
1326 struct pch_spi_dma_ctrl *dma;
1327
1328 dma = &data->dma;
1329 if (dma->tx_buf_dma)
1330 dma_free_coherent(&board_dat->pdev->dev, PCH_BUF_SIZE,
1331 dma->tx_buf_virt, dma->tx_buf_dma);
1332 if (dma->rx_buf_dma)
1333 dma_free_coherent(&board_dat->pdev->dev, PCH_BUF_SIZE,
1334 dma->rx_buf_virt, dma->rx_buf_dma);
1335 return;
1336}
1337
1338static void pch_alloc_dma_buf(struct pch_spi_board_data *board_dat,
1339 struct pch_spi_data *data)
1340{
1341 struct pch_spi_dma_ctrl *dma;
1342
1343 dma = &data->dma;
1344 /* Get Consistent memory for Tx DMA */
1345 dma->tx_buf_virt = dma_alloc_coherent(&board_dat->pdev->dev,
1346 PCH_BUF_SIZE, &dma->tx_buf_dma, GFP_KERNEL);
1347 /* Get Consistent memory for Rx DMA */
1348 dma->rx_buf_virt = dma_alloc_coherent(&board_dat->pdev->dev,
1349 PCH_BUF_SIZE, &dma->rx_buf_dma, GFP_KERNEL);
1350}
1351
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001352static int __devinit pch_spi_pd_probe(struct platform_device *plat_dev)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001353{
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001354 int ret;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001355 struct spi_master *master;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001356 struct pch_spi_board_data *board_dat = dev_get_platdata(&plat_dev->dev);
1357 struct pch_spi_data *data;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001358
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001359 dev_dbg(&plat_dev->dev, "%s:debug\n", __func__);
1360
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001361 master = spi_alloc_master(&board_dat->pdev->dev,
1362 sizeof(struct pch_spi_data));
1363 if (!master) {
1364 dev_err(&plat_dev->dev, "spi_alloc_master[%d] failed.\n",
1365 plat_dev->id);
1366 return -ENOMEM;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001367 }
1368
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001369 data = spi_master_get_devdata(master);
1370 data->master = master;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001371
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001372 platform_set_drvdata(plat_dev, data);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001373
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001374 /* baseaddress + address offset) */
1375 data->io_base_addr = pci_resource_start(board_dat->pdev, 1) +
1376 PCH_ADDRESS_SIZE * plat_dev->id;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001377 data->io_remap_addr = pci_iomap(board_dat->pdev, 1, 0) +
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001378 PCH_ADDRESS_SIZE * plat_dev->id;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001379 if (!data->io_remap_addr) {
1380 dev_err(&plat_dev->dev, "%s pci_iomap failed\n", __func__);
1381 ret = -ENOMEM;
1382 goto err_pci_iomap;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001383 }
1384
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001385 dev_dbg(&plat_dev->dev, "[ch%d] remap_addr=%p\n",
1386 plat_dev->id, data->io_remap_addr);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001387
1388 /* initialize members of SPI master */
1389 master->bus_num = -1;
1390 master->num_chipselect = PCH_MAX_CS;
1391 master->setup = pch_spi_setup;
1392 master->transfer = pch_spi_transfer;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001393
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001394 data->board_dat = board_dat;
1395 data->plat_dev = plat_dev;
1396 data->n_curnt_chip = 255;
1397 data->status = STATUS_RUNNING;
1398 data->ch = plat_dev->id;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001399 data->use_dma = use_dma;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001400
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001401 INIT_LIST_HEAD(&data->queue);
1402 spin_lock_init(&data->lock);
1403 INIT_WORK(&data->work, pch_spi_process_messages);
1404 init_waitqueue_head(&data->wait);
Grant Likely65308c42010-09-29 17:31:34 +09001405
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001406 ret = pch_spi_get_resources(board_dat, data);
1407 if (ret) {
1408 dev_err(&plat_dev->dev, "%s fail(retval=%d)\n", __func__, ret);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001409 goto err_spi_get_resources;
1410 }
1411
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001412 ret = request_irq(board_dat->pdev->irq, pch_spi_handler,
1413 IRQF_SHARED, KBUILD_MODNAME, data);
1414 if (ret) {
1415 dev_err(&plat_dev->dev,
1416 "%s request_irq failed\n", __func__);
1417 goto err_request_irq;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001418 }
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001419 data->irq_reg_sts = true;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001420
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001421 pch_spi_set_master_mode(master);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001422
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001423 ret = spi_register_master(master);
1424 if (ret != 0) {
1425 dev_err(&plat_dev->dev,
1426 "%s spi_register_master FAILED\n", __func__);
1427 goto err_spi_register_master;
1428 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001429
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001430 if (use_dma) {
1431 dev_info(&plat_dev->dev, "Use DMA for data transfers\n");
1432 pch_alloc_dma_buf(board_dat, data);
1433 }
1434
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001435 return 0;
1436
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001437err_spi_register_master:
1438 free_irq(board_dat->pdev->irq, board_dat);
1439err_request_irq:
1440 pch_spi_free_resources(board_dat, data);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001441err_spi_get_resources:
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001442 pci_iounmap(board_dat->pdev, data->io_remap_addr);
1443err_pci_iomap:
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001444 spi_master_put(master);
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001445
1446 return ret;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001447}
1448
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001449static int __devexit pch_spi_pd_remove(struct platform_device *plat_dev)
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001450{
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001451 struct pch_spi_board_data *board_dat = dev_get_platdata(&plat_dev->dev);
1452 struct pch_spi_data *data = platform_get_drvdata(plat_dev);
Grant Likely65308c42010-09-29 17:31:34 +09001453 int count;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001454 unsigned long flags;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001455
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001456 dev_dbg(&plat_dev->dev, "%s:[ch%d] irq=%d\n",
1457 __func__, plat_dev->id, board_dat->pdev->irq);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001458
1459 if (use_dma)
1460 pch_free_dma_buf(board_dat, data);
1461
Grant Likely65308c42010-09-29 17:31:34 +09001462 /* check for any pending messages; no action is taken if the queue
1463 * is still full; but at least we tried. Unload anyway */
1464 count = 500;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001465 spin_lock_irqsave(&data->lock, flags);
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001466 data->status = STATUS_EXITING;
1467 while ((list_empty(&data->queue) == 0) && --count) {
Grant Likely65308c42010-09-29 17:31:34 +09001468 dev_dbg(&board_dat->pdev->dev, "%s :queue not empty\n",
1469 __func__);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001470 spin_unlock_irqrestore(&data->lock, flags);
Grant Likely65308c42010-09-29 17:31:34 +09001471 msleep(PCH_SLEEP_TIME);
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001472 spin_lock_irqsave(&data->lock, flags);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001473 }
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001474 spin_unlock_irqrestore(&data->lock, flags);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001475
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001476 pch_spi_free_resources(board_dat, data);
1477 /* disable interrupts & free IRQ */
1478 if (data->irq_reg_sts) {
1479 /* disable interrupts */
1480 pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL);
1481 data->irq_reg_sts = false;
1482 free_irq(board_dat->pdev->irq, data);
1483 }
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001484
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001485 pci_iounmap(board_dat->pdev, data->io_remap_addr);
1486 spi_unregister_master(data->master);
1487 spi_master_put(data->master);
1488 platform_set_drvdata(plat_dev, NULL);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001489
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001490 return 0;
1491}
1492#ifdef CONFIG_PM
1493static int pch_spi_pd_suspend(struct platform_device *pd_dev,
1494 pm_message_t state)
1495{
1496 u8 count;
1497 struct pch_spi_board_data *board_dat = dev_get_platdata(&pd_dev->dev);
1498 struct pch_spi_data *data = platform_get_drvdata(pd_dev);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001499
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001500 dev_dbg(&pd_dev->dev, "%s ENTRY\n", __func__);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001501
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001502 if (!board_dat) {
1503 dev_err(&pd_dev->dev,
1504 "%s pci_get_drvdata returned NULL\n", __func__);
1505 return -EFAULT;
1506 }
1507
1508 /* check if the current message is processed:
1509 Only after thats done the transfer will be suspended */
1510 count = 255;
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001511 while ((--count) > 0) {
1512 if (!(data->bcurrent_msg_processing))
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001513 break;
1514 msleep(PCH_SLEEP_TIME);
1515 }
1516
1517 /* Free IRQ */
1518 if (data->irq_reg_sts) {
1519 /* disable all interrupts */
1520 pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL);
1521 pch_spi_reset(data->master);
1522 free_irq(board_dat->pdev->irq, data);
1523
1524 data->irq_reg_sts = false;
1525 dev_dbg(&pd_dev->dev,
1526 "%s free_irq invoked successfully.\n", __func__);
1527 }
1528
1529 return 0;
1530}
1531
1532static int pch_spi_pd_resume(struct platform_device *pd_dev)
1533{
1534 struct pch_spi_board_data *board_dat = dev_get_platdata(&pd_dev->dev);
1535 struct pch_spi_data *data = platform_get_drvdata(pd_dev);
1536 int retval;
1537
1538 if (!board_dat) {
1539 dev_err(&pd_dev->dev,
1540 "%s pci_get_drvdata returned NULL\n", __func__);
1541 return -EFAULT;
1542 }
1543
1544 if (!data->irq_reg_sts) {
1545 /* register IRQ */
1546 retval = request_irq(board_dat->pdev->irq, pch_spi_handler,
1547 IRQF_SHARED, KBUILD_MODNAME, data);
1548 if (retval < 0) {
1549 dev_err(&pd_dev->dev,
1550 "%s request_irq failed\n", __func__);
1551 return retval;
1552 }
1553
1554 /* reset PCH SPI h/w */
1555 pch_spi_reset(data->master);
1556 pch_spi_set_master_mode(data->master);
1557 data->irq_reg_sts = true;
1558 }
1559 return 0;
1560}
1561#else
1562#define pch_spi_pd_suspend NULL
1563#define pch_spi_pd_resume NULL
1564#endif
1565
1566static struct platform_driver pch_spi_pd_driver = {
1567 .driver = {
1568 .name = "pch-spi",
1569 .owner = THIS_MODULE,
1570 },
1571 .probe = pch_spi_pd_probe,
1572 .remove = __devexit_p(pch_spi_pd_remove),
1573 .suspend = pch_spi_pd_suspend,
1574 .resume = pch_spi_pd_resume
1575};
1576
1577static int __devinit pch_spi_probe(struct pci_dev *pdev,
1578 const struct pci_device_id *id)
1579{
1580 struct pch_spi_board_data *board_dat;
1581 struct platform_device *pd_dev = NULL;
1582 int retval;
1583 int i;
1584 struct pch_pd_dev_save *pd_dev_save;
1585
1586 pd_dev_save = kzalloc(sizeof(struct pch_pd_dev_save), GFP_KERNEL);
1587 if (!pd_dev_save) {
1588 dev_err(&pdev->dev, "%s Can't allocate pd_dev_sav\n", __func__);
1589 return -ENOMEM;
1590 }
1591
1592 board_dat = kzalloc(sizeof(struct pch_spi_board_data), GFP_KERNEL);
1593 if (!board_dat) {
1594 dev_err(&pdev->dev, "%s Can't allocate board_dat\n", __func__);
1595 retval = -ENOMEM;
1596 goto err_no_mem;
1597 }
1598
1599 retval = pci_request_regions(pdev, KBUILD_MODNAME);
1600 if (retval) {
1601 dev_err(&pdev->dev, "%s request_region failed\n", __func__);
1602 goto pci_request_regions;
1603 }
1604
1605 board_dat->pdev = pdev;
1606 board_dat->num = id->driver_data;
1607 pd_dev_save->num = id->driver_data;
1608 pd_dev_save->board_dat = board_dat;
1609
1610 retval = pci_enable_device(pdev);
1611 if (retval) {
1612 dev_err(&pdev->dev, "%s pci_enable_device failed\n", __func__);
1613 goto pci_enable_device;
1614 }
1615
1616 for (i = 0; i < board_dat->num; i++) {
1617 pd_dev = platform_device_alloc("pch-spi", i);
1618 if (!pd_dev) {
1619 dev_err(&pdev->dev, "platform_device_alloc failed\n");
1620 goto err_platform_device;
1621 }
1622 pd_dev_save->pd_save[i] = pd_dev;
1623 pd_dev->dev.parent = &pdev->dev;
1624
1625 retval = platform_device_add_data(pd_dev, board_dat,
1626 sizeof(*board_dat));
1627 if (retval) {
1628 dev_err(&pdev->dev,
1629 "platform_device_add_data failed\n");
1630 platform_device_put(pd_dev);
1631 goto err_platform_device;
1632 }
1633
1634 retval = platform_device_add(pd_dev);
1635 if (retval) {
1636 dev_err(&pdev->dev, "platform_device_add failed\n");
1637 platform_device_put(pd_dev);
1638 goto err_platform_device;
1639 }
1640 }
1641
1642 pci_set_drvdata(pdev, pd_dev_save);
1643
1644 return 0;
1645
1646err_platform_device:
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001647 pci_disable_device(pdev);
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001648pci_enable_device:
1649 pci_release_regions(pdev);
1650pci_request_regions:
1651 kfree(board_dat);
1652err_no_mem:
1653 kfree(pd_dev_save);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001654
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001655 return retval;
1656}
1657
1658static void __devexit pch_spi_remove(struct pci_dev *pdev)
1659{
1660 int i;
1661 struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);
1662
1663 dev_dbg(&pdev->dev, "%s ENTRY:pdev=%p\n", __func__, pdev);
1664
1665 for (i = 0; i < pd_dev_save->num; i++)
1666 platform_device_unregister(pd_dev_save->pd_save[i]);
1667
1668 pci_disable_device(pdev);
1669 pci_release_regions(pdev);
1670 kfree(pd_dev_save->board_dat);
1671 kfree(pd_dev_save);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001672}
1673
1674#ifdef CONFIG_PM
1675static int pch_spi_suspend(struct pci_dev *pdev, pm_message_t state)
1676{
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001677 int retval;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001678 struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001679
1680 dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
1681
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001682 pd_dev_save->board_dat->suspend_sts = true;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001683
1684 /* save config space */
1685 retval = pci_save_state(pdev);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001686 if (retval == 0) {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001687 pci_enable_wake(pdev, PCI_D3hot, 0);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001688 pci_disable_device(pdev);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001689 pci_set_power_state(pdev, PCI_D3hot);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001690 } else {
1691 dev_err(&pdev->dev, "%s pci_save_state failed\n", __func__);
1692 }
1693
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001694 return retval;
1695}
1696
1697static int pch_spi_resume(struct pci_dev *pdev)
1698{
1699 int retval;
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001700 struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001701 dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
1702
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001703 pci_set_power_state(pdev, PCI_D0);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001704 pci_restore_state(pdev);
1705
1706 retval = pci_enable_device(pdev);
1707 if (retval < 0) {
1708 dev_err(&pdev->dev,
1709 "%s pci_enable_device failed\n", __func__);
1710 } else {
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001711 pci_enable_wake(pdev, PCI_D3hot, 0);
1712
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001713 /* set suspend status to false */
1714 pd_dev_save->board_dat->suspend_sts = false;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001715 }
1716
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001717 return retval;
1718}
1719#else
1720#define pch_spi_suspend NULL
1721#define pch_spi_resume NULL
1722
1723#endif
1724
1725static struct pci_driver pch_spi_pcidev = {
1726 .name = "pch_spi",
1727 .id_table = pch_spi_pcidev_id,
1728 .probe = pch_spi_probe,
1729 .remove = pch_spi_remove,
1730 .suspend = pch_spi_suspend,
1731 .resume = pch_spi_resume,
1732};
1733
1734static int __init pch_spi_init(void)
1735{
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001736 int ret;
1737 ret = platform_driver_register(&pch_spi_pd_driver);
1738 if (ret)
1739 return ret;
1740
1741 ret = pci_register_driver(&pch_spi_pcidev);
1742 if (ret)
1743 return ret;
1744
1745 return 0;
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001746}
1747module_init(pch_spi_init);
1748
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001749static void __exit pch_spi_exit(void)
1750{
1751 pci_unregister_driver(&pch_spi_pcidev);
Tomoya MORINAGAf016aeb2011-06-07 14:50:10 +09001752 platform_driver_unregister(&pch_spi_pd_driver);
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001753}
1754module_exit(pch_spi_exit);
1755
Tomoya MORINAGAc37f3c22011-06-07 14:50:11 +09001756module_param(use_dma, int, 0644);
1757MODULE_PARM_DESC(use_dma,
1758 "to use DMA for data transfers pass 1 else 0; default 1");
1759
Masayuki Ohtakee8b17b52010-10-08 12:44:49 -06001760MODULE_LICENSE("GPL");
Tomoya MORINAGA2b246282011-10-28 09:35:22 +09001761MODULE_DESCRIPTION("Intel EG20T PCH/LAPIS Semiconductor ML7xxx IOH SPI Driver");