29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
35 #include <linux/device.h>
43 #define DRV_NAME "pata-bf54x"
44 #define DRV_VERSION "0.9"
46 #define ATA_REG_CTRL 0x0E
47 #define ATA_REG_ALTSTATUS ATA_REG_CTRL
50 #define ATAPI_OFFSET_CONTROL 0x00
51 #define ATAPI_OFFSET_STATUS 0x04
52 #define ATAPI_OFFSET_DEV_ADDR 0x08
53 #define ATAPI_OFFSET_DEV_TXBUF 0x0c
54 #define ATAPI_OFFSET_DEV_RXBUF 0x10
55 #define ATAPI_OFFSET_INT_MASK 0x14
56 #define ATAPI_OFFSET_INT_STATUS 0x18
57 #define ATAPI_OFFSET_XFER_LEN 0x1c
58 #define ATAPI_OFFSET_LINE_STATUS 0x20
59 #define ATAPI_OFFSET_SM_STATE 0x24
60 #define ATAPI_OFFSET_TERMINATE 0x28
61 #define ATAPI_OFFSET_PIO_TFRCNT 0x2c
62 #define ATAPI_OFFSET_DMA_TFRCNT 0x30
63 #define ATAPI_OFFSET_UMAIN_TFRCNT 0x34
64 #define ATAPI_OFFSET_UDMAOUT_TFRCNT 0x38
65 #define ATAPI_OFFSET_REG_TIM_0 0x40
66 #define ATAPI_OFFSET_PIO_TIM_0 0x44
67 #define ATAPI_OFFSET_PIO_TIM_1 0x48
68 #define ATAPI_OFFSET_MULTI_TIM_0 0x50
69 #define ATAPI_OFFSET_MULTI_TIM_1 0x54
70 #define ATAPI_OFFSET_MULTI_TIM_2 0x58
71 #define ATAPI_OFFSET_ULTRA_TIM_0 0x60
72 #define ATAPI_OFFSET_ULTRA_TIM_1 0x64
73 #define ATAPI_OFFSET_ULTRA_TIM_2 0x68
74 #define ATAPI_OFFSET_ULTRA_TIM_3 0x6c
77 #define ATAPI_GET_CONTROL(base)\
78 bfin_read16(base + ATAPI_OFFSET_CONTROL)
79 #define ATAPI_SET_CONTROL(base, val)\
80 bfin_write16(base + ATAPI_OFFSET_CONTROL, val)
81 #define ATAPI_GET_STATUS(base)\
82 bfin_read16(base + ATAPI_OFFSET_STATUS)
83 #define ATAPI_GET_DEV_ADDR(base)\
84 bfin_read16(base + ATAPI_OFFSET_DEV_ADDR)
85 #define ATAPI_SET_DEV_ADDR(base, val)\
86 bfin_write16(base + ATAPI_OFFSET_DEV_ADDR, val)
87 #define ATAPI_GET_DEV_TXBUF(base)\
88 bfin_read16(base + ATAPI_OFFSET_DEV_TXBUF)
89 #define ATAPI_SET_DEV_TXBUF(base, val)\
90 bfin_write16(base + ATAPI_OFFSET_DEV_TXBUF, val)
91 #define ATAPI_GET_DEV_RXBUF(base)\
92 bfin_read16(base + ATAPI_OFFSET_DEV_RXBUF)
93 #define ATAPI_SET_DEV_RXBUF(base, val)\
94 bfin_write16(base + ATAPI_OFFSET_DEV_RXBUF, val)
95 #define ATAPI_GET_INT_MASK(base)\
96 bfin_read16(base + ATAPI_OFFSET_INT_MASK)
97 #define ATAPI_SET_INT_MASK(base, val)\
98 bfin_write16(base + ATAPI_OFFSET_INT_MASK, val)
99 #define ATAPI_GET_INT_STATUS(base)\
100 bfin_read16(base + ATAPI_OFFSET_INT_STATUS)
101 #define ATAPI_SET_INT_STATUS(base, val)\
102 bfin_write16(base + ATAPI_OFFSET_INT_STATUS, val)
103 #define ATAPI_GET_XFER_LEN(base)\
104 bfin_read16(base + ATAPI_OFFSET_XFER_LEN)
105 #define ATAPI_SET_XFER_LEN(base, val)\
106 bfin_write16(base + ATAPI_OFFSET_XFER_LEN, val)
107 #define ATAPI_GET_LINE_STATUS(base)\
108 bfin_read16(base + ATAPI_OFFSET_LINE_STATUS)
109 #define ATAPI_GET_SM_STATE(base)\
110 bfin_read16(base + ATAPI_OFFSET_SM_STATE)
111 #define ATAPI_GET_TERMINATE(base)\
112 bfin_read16(base + ATAPI_OFFSET_TERMINATE)
113 #define ATAPI_SET_TERMINATE(base, val)\
114 bfin_write16(base + ATAPI_OFFSET_TERMINATE, val)
115 #define ATAPI_GET_PIO_TFRCNT(base)\
116 bfin_read16(base + ATAPI_OFFSET_PIO_TFRCNT)
117 #define ATAPI_GET_DMA_TFRCNT(base)\
118 bfin_read16(base + ATAPI_OFFSET_DMA_TFRCNT)
119 #define ATAPI_GET_UMAIN_TFRCNT(base)\
120 bfin_read16(base + ATAPI_OFFSET_UMAIN_TFRCNT)
121 #define ATAPI_GET_UDMAOUT_TFRCNT(base)\
122 bfin_read16(base + ATAPI_OFFSET_UDMAOUT_TFRCNT)
123 #define ATAPI_GET_REG_TIM_0(base)\
124 bfin_read16(base + ATAPI_OFFSET_REG_TIM_0)
125 #define ATAPI_SET_REG_TIM_0(base, val)\
126 bfin_write16(base + ATAPI_OFFSET_REG_TIM_0, val)
127 #define ATAPI_GET_PIO_TIM_0(base)\
128 bfin_read16(base + ATAPI_OFFSET_PIO_TIM_0)
129 #define ATAPI_SET_PIO_TIM_0(base, val)\
130 bfin_write16(base + ATAPI_OFFSET_PIO_TIM_0, val)
131 #define ATAPI_GET_PIO_TIM_1(base)\
132 bfin_read16(base + ATAPI_OFFSET_PIO_TIM_1)
133 #define ATAPI_SET_PIO_TIM_1(base, val)\
134 bfin_write16(base + ATAPI_OFFSET_PIO_TIM_1, val)
135 #define ATAPI_GET_MULTI_TIM_0(base)\
136 bfin_read16(base + ATAPI_OFFSET_MULTI_TIM_0)
137 #define ATAPI_SET_MULTI_TIM_0(base, val)\
138 bfin_write16(base + ATAPI_OFFSET_MULTI_TIM_0, val)
139 #define ATAPI_GET_MULTI_TIM_1(base)\
140 bfin_read16(base + ATAPI_OFFSET_MULTI_TIM_1)
141 #define ATAPI_SET_MULTI_TIM_1(base, val)\
142 bfin_write16(base + ATAPI_OFFSET_MULTI_TIM_1, val)
143 #define ATAPI_GET_MULTI_TIM_2(base)\
144 bfin_read16(base + ATAPI_OFFSET_MULTI_TIM_2)
145 #define ATAPI_SET_MULTI_TIM_2(base, val)\
146 bfin_write16(base + ATAPI_OFFSET_MULTI_TIM_2, val)
147 #define ATAPI_GET_ULTRA_TIM_0(base)\
148 bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_0)
149 #define ATAPI_SET_ULTRA_TIM_0(base, val)\
150 bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_0, val)
151 #define ATAPI_GET_ULTRA_TIM_1(base)\
152 bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_1)
153 #define ATAPI_SET_ULTRA_TIM_1(base, val)\
154 bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_1, val)
155 #define ATAPI_GET_ULTRA_TIM_2(base)\
156 bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_2)
157 #define ATAPI_SET_ULTRA_TIM_2(base, val)\
158 bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_2, val)
159 #define ATAPI_GET_ULTRA_TIM_3(base)\
160 bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_3)
161 #define ATAPI_SET_ULTRA_TIM_3(base, val)\
162 bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_3, val)
168 static const u32 pio_fsclk[] =
169 { 33333333, 33333333, 33333333, 33333333, 33333333 };
175 static const u32 mdma_fsclk[] = { 33333333, 33333333, 33333333 };
186 static const u32 udma_fsclk[] =
187 { 33333333, 33333333, 40000000, 50000000, 80000000, 133333333 };
194 static const u32 reg_t0min[] = { 600, 383, 330, 180, 120 };
196 static const u32 reg_t2min[] = { 290, 290, 290, 70, 25 };
198 static const u32 reg_teocmin[] = { 290, 290, 290, 80, 70 };
205 static const u32 pio_t0min[] = { 600, 383, 240, 180, 120 };
207 static const u32 pio_t1min[] = { 70, 50, 30, 30, 25 };
209 static const u32 pio_t2min[] = { 165, 125, 100, 80, 70 };
211 static const u32 pio_teocmin[] = { 165, 125, 100, 70, 25 };
213 static const u32 pio_t4min[] = { 30, 20, 15, 10, 10 };
221 static const u32 mdma_t0min[] = { 480, 150, 120 };
223 static const u32 mdma_tdmin[] = { 215, 80, 70 };
225 static const u32 mdma_thmin[] = { 20, 15, 10 };
227 static const u32 mdma_tjmin[] = { 20, 5, 5 };
229 static const u32 mdma_tkrmin[] = { 50, 50, 25 };
231 static const u32 mdma_tkwmin[] = { 215, 50, 25 };
233 static const u32 mdma_tmmin[] = { 50, 30, 25 };
235 static const u32 mdma_tzmax[] = { 20, 25, 25 };
241 static const u32 udma_tcycmin[] = { 112, 73, 54, 39, 25, 17 };
242 static const u32 udma_tdvsmin[] = { 70, 48, 31, 20, 7, 5 };
243 static const u32 udma_tenvmax[] = { 70, 70, 70, 55, 55, 50 };
244 static const u32 udma_trpmin[] = { 160, 125, 100, 100, 100, 85 };
245 static const u32 udma_tmin[] = { 5, 5, 5, 5, 3, 3 };
248 static const u32 udma_tmlimin = 20;
249 static const u32 udma_tzahmin = 20;
250 static const u32 udma_tenvmin = 20;
251 static const u32 udma_tackmin = 20;
252 static const u32 udma_tssmin = 50;
254 #define BFIN_MAX_SG_SEGMENTS 4
263 static unsigned short num_clocks_min(
unsigned long tmin,
269 tmp = tmin * (fsclk/1000/1000) / 1000;
270 result = (
unsigned short)tmp;
271 if ((tmp*1000*1000) < (tmin*(fsclk/1000))) {
294 unsigned short teoc_reg, t2_reg, teoc_pio;
295 unsigned short t4_reg, t2_pio, t1_reg;
296 unsigned short n0, n6, t6min = 5;
302 n6 = num_clocks_min(t6min, fsclk);
303 if (mode >= 0 && mode <= 4 && n6 >= 1) {
304 dev_dbg(adev->
link->ap->dev,
"set piomode: mode=%d, fsclk=%ud\n", mode, fsclk);
306 while (mode > 0 && pio_fsclk[mode] > fsclk)
310 t2_reg = num_clocks_min(reg_t2min[mode], fsclk);
312 teoc_reg = num_clocks_min(reg_teocmin[mode], fsclk);
314 n0 = num_clocks_min(reg_t0min[mode], fsclk);
317 if (t2_reg + teoc_reg < n0)
318 t2_reg = n0 - teoc_reg;
323 t2_pio = num_clocks_min(pio_t2min[mode], fsclk);
325 teoc_pio = num_clocks_min(pio_teocmin[mode], fsclk);
327 n0 = num_clocks_min(pio_t0min[mode], fsclk);
330 if (t2_pio + teoc_pio < n0)
331 t2_pio = n0 - teoc_pio;
334 t1_reg = num_clocks_min(pio_t1min[mode], fsclk);
337 t4_reg = num_clocks_min(pio_t4min[mode], fsclk);
373 unsigned short tenv, tack, tcyc_tdvs, tdvs, tmli,
tss, trp, tzah;
374 unsigned short tm,
td, tkr, tkw, teoc,
th;
375 unsigned short n0, nf, tfmin = 5;
376 unsigned short nmin, tcyc;
379 if (mode >= 0 && mode <= 5) {
380 dev_dbg(adev->
link->ap->dev,
"set udmamode: mode=%d\n", mode);
386 while (mode > 0 && udma_fsclk[mode] > fsclk)
389 nmin = num_clocks_min(udma_tmin[mode], fsclk);
392 tdvs = num_clocks_min(udma_tdvsmin[mode], fsclk);
393 tcyc = num_clocks_min(udma_tcycmin[mode], fsclk);
399 if (tdvs + tcyc_tdvs < tcyc)
400 tcyc_tdvs = tcyc - tdvs;
411 tack = num_clocks_min(udma_tackmin, fsclk);
412 tss = num_clocks_min(udma_tssmin, fsclk);
413 tmli = num_clocks_min(udma_tmlimin, fsclk);
414 tzah = num_clocks_min(udma_tzahmin, fsclk);
415 trp = num_clocks_min(udma_trpmin[mode], fsclk);
416 tenv = num_clocks_min(udma_tenvmin, fsclk);
417 if (tenv <= udma_tenvmax[mode]) {
420 (tcyc_tdvs<<8 | tdvs));
428 if (mode >= 0 && mode <= 2) {
429 dev_dbg(adev->
link->ap->dev,
"set mdmamode: mode=%d\n", mode);
435 while (mode > 0 && mdma_fsclk[mode] > fsclk)
438 nf = num_clocks_min(tfmin, fsclk);
443 td = num_clocks_min(mdma_tdmin[mode], fsclk);
446 tkw = num_clocks_min(mdma_tkwmin[mode], fsclk);
449 n0 = num_clocks_min(mdma_t0min[mode], fsclk);
456 tkr = num_clocks_min(mdma_tkrmin[mode], fsclk);
458 tm = num_clocks_min(mdma_tmmin[mode], fsclk);
460 teoc = num_clocks_min(mdma_tjmin[mode], fsclk);
462 th = num_clocks_min(mdma_thmin[mode], fsclk);
480 static inline void wait_complete(
void __iomem *base,
unsigned short mask)
485 #define PATA_BF54X_WAIT_TIMEOUT 10000
504 static void write_atapi_register(
void __iomem *base,
505 unsigned long ata_reg,
unsigned short value)
541 static unsigned short read_atapi_register(
void __iomem *base,
542 unsigned long ata_reg)
579 static void write_atapi_data(
void __iomem *base,
580 int len,
unsigned short *
buf)
599 for (i = 0; i < len; i++) {
624 static void read_atapi_data(
void __iomem *base,
625 int len,
unsigned short *buf)
644 for (i = 0; i < len; i++) {
674 if (tf->
ctl != ap->last_ctl) {
676 ap->last_ctl = tf->
ctl;
689 dev_dbg(ap->
dev,
"hob: feat 0x%X nsect 0x%X, lba 0x%X "
703 dev_dbg(ap->
dev,
"feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
726 static u8 bfin_check_status(
struct ata_port *ap)
744 tf->
command = bfin_check_status(ap);
770 static void bfin_exec_command(
struct ata_port *ap,
785 static u8 bfin_check_altstatus(
struct ata_port *ap)
799 static void bfin_dev_select(
struct ata_port *ap,
unsigned int device)
842 unsigned int size = 0;
844 dev_dbg(qc->
ap->dev,
"in atapi dma setup\n");
870 (
unsigned int)dma_desc_cpu +
874 set_dma_curr_desc_addr(channel, (
unsigned long *)ap->bmdma_prd_dma);
875 set_dma_x_count(channel, 0);
876 set_dma_x_modify(channel, 0);
877 set_dma_config(channel, config);
882 bfin_exec_command(ap, &qc->
tf);
916 dev_dbg(qc->
ap->dev,
"in atapi dma start\n");
940 dev_dbg(qc->
ap->dev,
"in atapi dma stop\n");
965 static unsigned int bfin_devchk(
struct ata_port *ap,
971 bfin_dev_select(ap, device);
985 if ((nsect == 0x55) && (
lbal == 0xaa))
997 static void bfin_bus_post_reset(
struct ata_port *ap,
unsigned int devmask)
1000 unsigned int dev0 = devmask & (1 << 0);
1001 unsigned int dev1 = devmask & (1 << 1);
1002 unsigned long deadline;
1017 bfin_dev_select(ap, 1);
1020 if ((nsect == 1) && (lbal == 1))
1032 bfin_dev_select(ap, 0);
1034 bfin_dev_select(ap, 1);
1036 bfin_dev_select(ap, 0);
1045 static unsigned int bfin_bus_softreset(
struct ata_port *ap,
1046 unsigned int devmask)
1073 if (bfin_check_status(ap) == 0xFF)
1076 bfin_bus_post_reset(ap, devmask);
1089 static int bfin_softreset(
struct ata_link *
link,
unsigned int *classes,
1090 unsigned long deadline)
1094 unsigned int devmask = 0, err_mask;
1098 if (bfin_devchk(ap, 0))
1099 devmask |= (1 << 0);
1100 if (slave_possible && bfin_devchk(ap, 1))
1101 devmask |= (1 << 1);
1104 bfin_dev_select(ap, 0);
1107 err_mask = bfin_bus_softreset(ap, devmask);
1116 devmask & (1 << 0), &err);
1117 if (slave_possible && err != 0x81)
1119 devmask & (1 << 1), &err);
1129 static unsigned char bfin_bmdma_status(
struct ata_port *ap)
1131 unsigned char host_stat = 0;
1139 dev_dbg(ap->
dev,
"ATAPI: host_stat=0x%x\n", host_stat);
1154 static unsigned int bfin_data_xfer(
struct ata_device *
dev,
unsigned char *buf,
1159 unsigned int words = buflen >> 1;
1160 unsigned short *buf16 = (
u16 *)
buf;
1164 read_atapi_data(base, words, buf16);
1166 write_atapi_data(base, words, buf16);
1170 unsigned short align_buf[1] = { 0 };
1171 unsigned char *trailing_buf = buf + buflen - 1;
1174 read_atapi_data(base, 1, align_buf);
1175 memcpy(trailing_buf, align_buf, 1);
1177 memcpy(align_buf, trailing_buf, 1);
1178 write_atapi_data(base, 1, align_buf);
1193 static void bfin_irq_clear(
struct ata_port *ap)
1213 bfin_check_status(ap);
1225 static void bfin_postreset(
struct ata_link *link,
unsigned int *classes)
1235 bfin_dev_select(ap, 1);
1237 bfin_dev_select(ap, 0);
1248 static void bfin_port_stop(
struct ata_port *ap)
1262 static int bfin_port_start(
struct ata_port *ap)
1273 if (ap->bmdma_prd ==
NULL) {
1274 dev_info(ap->
dev,
"Unable to allocate DMA descriptor array.\n");
1280 "BFIN ATAPI TX DMA") >= 0)
1293 dev_err(ap->
dev,
"Unable to request ATAPI DMA!"
1294 " Continue in PIO mode.\n");
1299 static unsigned int bfin_ata_host_intr(
struct ata_port *ap,
1305 VPRINTK(
"ata%u: protocol %d task_state %d\n",
1326 host_stat = ap->
ops->bmdma_status(ap);
1327 VPRINTK(
"ata%u: host_stat 0x%X\n",
1335 ap->
ops->bmdma_stop(qc);
1351 status = ap->
ops->sff_check_altstatus(ap);
1356 status = ap->
ops->sff_check_status(ap);
1361 ap->
ops->sff_irq_clear(ap);
1373 ap->
stats.idle_irq++;
1376 if ((ap->
stats.idle_irq % 1000) == 0) {
1377 ap->
ops->irq_ack(ap, 0);
1385 static irqreturn_t bfin_ata_interrupt(
int irq,
void *dev_instance)
1389 unsigned int handled = 0;
1390 unsigned long flags;
1395 for (i = 0; i < host->
n_ports; i++) {
1399 qc = ata_qc_from_tag(ap, ap->
link.active_tag);
1401 handled |= bfin_ata_host_intr(ap, qc);
1404 spin_unlock_irqrestore(&host->
lock, flags);
1417 .inherits = &ata_bmdma_port_ops,
1419 .set_piomode = bfin_set_piomode,
1420 .set_dmamode = bfin_set_dmamode,
1422 .sff_tf_load = bfin_tf_load,
1423 .sff_tf_read = bfin_tf_read,
1424 .sff_exec_command = bfin_exec_command,
1425 .sff_check_status = bfin_check_status,
1426 .sff_check_altstatus = bfin_check_altstatus,
1427 .sff_dev_select = bfin_dev_select,
1428 .sff_set_devctl = bfin_set_devctl,
1430 .bmdma_setup = bfin_bmdma_setup,
1431 .bmdma_start = bfin_bmdma_start,
1432 .bmdma_stop = bfin_bmdma_stop,
1433 .bmdma_status = bfin_bmdma_status,
1434 .sff_data_xfer = bfin_data_xfer,
1439 .softreset = bfin_softreset,
1440 .postreset = bfin_postreset,
1442 .sff_irq_clear = bfin_irq_clear,
1444 .port_start = bfin_port_start,
1445 .port_stop = bfin_port_stop,
1454 .port_ops = &bfin_pata_ops,
1462 static int bfin_reset_controller(
struct ata_host *host)
1466 unsigned short status;
1484 }
while (--
count && (status & ATA_BUSY));
1496 static unsigned short atapi_io_port[] = {
1549 { &bfin_port_info[board_idx],
NULL };
1555 dev_err(&pdev->
dev,
"invalid number of resources\n");
1566 while (bfin_port_info[board_idx].
udma_mask > 0 &&
1567 udma_fsclk[udma_mode] > fsclk) {
1569 bfin_port_info[board_idx].
udma_mask >>= 1;
1579 host->
ports[0]->ioaddr.ctl_addr = (
void *)res->
start;
1582 dev_err(&pdev->
dev,
"Requesting Peripherals failed\n");
1586 if (bfin_reset_controller(host)) {
1588 dev_err(&pdev->
dev,
"Fail to reset ATAPI device\n");
1593 bfin_ata_interrupt,
IRQF_SHARED, &bfin_sht) != 0) {
1595 dev_err(&pdev->
dev,
"Fail to attach ATAPI device\n");
1613 struct device *dev = &pdev->
dev;
1629 return ata_host_suspend(host, state);
1640 ret = bfin_reset_controller(host);
1645 ata_host_resume(host);
1651 #define bfin_atapi_suspend NULL
1652 #define bfin_atapi_resume NULL
1656 .probe = bfin_atapi_probe,
1666 #define ATAPI_MODE_SIZE 10
1669 static int __init bfin_atapi_init(
void)
1671 pr_info(
"register bfin atapi driver\n");
1673 switch(bfin_atapi_mode[0]) {
1688 static void __exit bfin_atapi_exit(
void)