-
Notifications
You must be signed in to change notification settings - Fork 15
/
pata-gpio.c
638 lines (524 loc) · 15.7 KB
/
pata-gpio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/kernel.h>
#include <linux/libata.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/printk.h>
#include <scsi/scsi_host.h>
// CS0 High / CS1 Low
#define REG_DATA 0x00
#define REG_ERROR 0x01
#define REG_FEATURE 0x01
#define REG_NSECT 0x02
#define REG_LBAL 0x03
#define REG_LBAM 0x04
#define REG_LBAH 0x05
#define REG_DEVICE 0x06
#define REG_STATUS 0x07
#define REG_COMMAND 0x07
// CS1 High / CS0 Low
#define REG_ALTSTATUS 0x10
#define REG_CTL 0x10
// Invalid register cache value
#define REG_INVALID 0xff
#if 0
#define pata_ndelay(x) ndelay(x)
#else
#define pata_ndelay(x)
#endif
struct pata_gpio {
struct device *dev;
struct gpio_descs *databus_gpios;
struct gpio_desc *reset_gpio;
struct gpio_descs *cs_gpios;
struct gpio_descs *address_gpios;
struct gpio_desc *strobe_write_gpio;
struct gpio_desc *strobe_read_gpio;
u8 last_reg;
const struct ata_timing *timing;
};
static int pata_gpio_set_register(struct pata_gpio *pata, unsigned long reg)
{
int err;
unsigned long cs_state = 0b01;
if (pata->last_reg == reg) {
return 0;
}
if (reg & 0xF0)
cs_state = 0b10;
err = gpiod_set_array_value(pata->cs_gpios->ndescs,
pata->cs_gpios->desc,
pata->cs_gpios->info,
&cs_state);
if (err)
return err;
err = gpiod_set_array_value(pata->address_gpios->ndescs,
pata->address_gpios->desc,
pata->address_gpios->info,
®);
if (err)
return err;
pata->last_reg = reg;
return 0;
}
static int __pata_gpio_read16_no_iocfg(struct pata_gpio *pata, u8 reg, u16 *result) {
int err;
unsigned long value = 0;
err = pata_gpio_set_register(pata, reg);
if (err)
return err;
if (pata->timing)
pata_ndelay(pata->timing->setup);
gpiod_set_value(pata->strobe_read_gpio, 1);
if (pata->timing)
pata_ndelay(pata->timing->act8b);
err = gpiod_get_array_value(pata->databus_gpios->ndescs,
pata->databus_gpios->desc,
pata->databus_gpios->info,
&value);
gpiod_set_value(pata->strobe_read_gpio, 0);
if (!err)
*result = value;
return err;
}
static int pata_gpio_read16(struct pata_gpio *pata, u8 reg, u16 *result)
{
u8 i;
int err;
for (i = 0; i < pata->databus_gpios->ndescs; i++) {
err = gpiod_direction_input(pata->databus_gpios->desc[i]);
if (err)
return err;
}
return __pata_gpio_read16_no_iocfg(pata, reg, result);
}
static int __pata_gpio_write16_no_iocfg(struct pata_gpio *pata, unsigned long value)
{
int err;
err = gpiod_set_array_value(pata->databus_gpios->ndescs, pata->databus_gpios->desc, pata->databus_gpios->info, &value);
if (err)
return err;
if (pata->timing)
pata_ndelay(pata->timing->setup);
gpiod_set_value(pata->strobe_write_gpio, 1);
if (pata->timing)
pata_ndelay(pata->timing->act8b);
gpiod_set_value(pata->strobe_write_gpio, 0);
if (pata->timing)
pata_ndelay(pata->timing->rec8b);
return 0;
}
static int pata_gpio_write16(struct pata_gpio *pata, u8 reg, unsigned long value)
{
u8 i;
int err;
err = pata_gpio_set_register(pata, reg);
if (err)
return err;
if (pata->timing)
pata_ndelay(pata->timing->setup);
for (i = 0; i < pata->databus_gpios->ndescs; i++) {
err = gpiod_direction_output(pata->databus_gpios->desc[i], (value >> i) & 0x01);
if (err)
return err;
}
gpiod_set_value(pata->strobe_write_gpio, 1);
if (pata->timing)
pata_ndelay(pata->timing->act8b);
gpiod_set_value(pata->strobe_write_gpio, 0);
if (pata->timing)
pata_ndelay(pata->timing->rec8b);
for (i = 0; i < pata->databus_gpios->ndescs; i++) {
err = gpiod_direction_input(pata->databus_gpios->desc[i]);
if (err)
return err;
}
return 0;
}
static void pata_gpio_write16_safe(struct pata_gpio *pata, u8 reg, unsigned long value)
{
int err;
err = pata_gpio_write16(pata, reg, value);
if (err) {
dev_err(pata->dev, "failed to write gpios in %s, code %d\n", __func__, err);
BUG();
}
}
static u16 pata_gpio_read16_safe(struct pata_gpio *pata, u8 reg)
{
u16 result;
int err;
err = pata_gpio_read16(pata, reg, &result);
if (err) {
dev_err(pata->dev, "failed to read gpios in %s, code %d\n", __func__, err);
BUG();
}
return result;
}
/*
* pata_gpio_check_status - Read device status register
*/
static u8 pata_gpio_check_status(struct ata_port *ap)
{
return pata_gpio_read16_safe(ap->host->private_data, REG_STATUS) & 0xFF;
}
/*
* pata_gpio_check_altstatus - Read alternate device status register
*/
static u8 pata_gpio_check_altstatus(struct ata_port *ap)
{
return pata_gpio_read16_safe(ap->host->private_data, REG_ALTSTATUS) & 0xFF;
}
/*
* pata_gpio_exec_command - issue ATA command to host controller
*/
static void pata_gpio_exec_command(struct ata_port *ap,
const struct ata_taskfile *tf)
{
pata_gpio_write16_safe(ap->host->private_data, REG_COMMAND, tf->command);
ata_sff_pause(ap);
}
/*
* pata_gpio_tf_load - send taskfile registers to host controller
*/
static void pata_gpio_tf_load(struct ata_port *ap,
const struct ata_taskfile *tf)
{
struct pata_gpio *pata = ap->host->private_data;
unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
if (tf->ctl != ap->last_ctl) {
pata_gpio_write16_safe(ap->host->private_data, REG_CTL, tf->ctl);
ap->last_ctl = tf->ctl;
ata_wait_idle(ap);
}
if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
pata_gpio_write16_safe(pata, REG_FEATURE, tf->hob_feature);
pata_gpio_write16_safe(pata, REG_NSECT, tf->hob_nsect);
pata_gpio_write16_safe(pata, REG_LBAL, tf->hob_lbal);
pata_gpio_write16_safe(pata, REG_LBAM, tf->hob_lbam);
pata_gpio_write16_safe(pata, REG_LBAH, tf->hob_lbah);
}
if (is_addr) {
pata_gpio_write16_safe(pata, REG_FEATURE, tf->feature);
pata_gpio_write16_safe(pata, REG_NSECT, tf->nsect);
pata_gpio_write16_safe(pata, REG_LBAL, tf->lbal);
pata_gpio_write16_safe(pata, REG_LBAM, tf->lbam);
pata_gpio_write16_safe(pata, REG_LBAH, tf->lbah);
}
if (tf->flags & ATA_TFLAG_DEVICE)
pata_gpio_write16_safe(pata, REG_DEVICE, tf->device);
ata_wait_idle(ap);
}
/*
* pata_gpio_tf_read - input device's ATA taskfile shadow registers
*/
static void pata_gpio_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
{
struct pata_gpio *pata = ap->host->private_data;
tf->feature = pata_gpio_read16_safe(pata, REG_FEATURE);
tf->nsect = pata_gpio_read16_safe(pata, REG_NSECT);
tf->lbal = pata_gpio_read16_safe(pata, REG_LBAL);
tf->lbam = pata_gpio_read16_safe(pata, REG_LBAM);
tf->lbah = pata_gpio_read16_safe(pata, REG_LBAH);
tf->device = pata_gpio_read16_safe(pata, REG_DEVICE);
if (tf->flags & ATA_TFLAG_LBA48) {
pata_gpio_write16_safe(pata, REG_CTL, tf->ctl | ATA_HOB);
tf->hob_feature = pata_gpio_read16_safe(pata, REG_FEATURE);
tf->hob_nsect = pata_gpio_read16_safe(pata, REG_NSECT);
tf->hob_lbal = pata_gpio_read16_safe(pata, REG_LBAL);
tf->hob_lbam = pata_gpio_read16_safe(pata, REG_LBAM);
tf->hob_lbah = pata_gpio_read16_safe(pata, REG_LBAH);
pata_gpio_write16_safe(pata, REG_CTL, tf->ctl);
ap->last_ctl = tf->ctl;
}
}
/*
* pata_gpio_data_xfer - Transfer data by PIO
*/
static unsigned int pata_gpio_data_xfer(struct ata_queued_cmd *qc,
unsigned char *buf, unsigned int buflen, int rw)
{
unsigned int i;
unsigned int words = buflen >> 1;
struct ata_port *ap = qc->dev->link->ap;
struct pata_gpio *pata = ap->host->private_data;
u16 *buf16 = (u16 *) buf;
int err;
/* Transfer multiple of 2 bytes */
if (rw == READ) {
buf16[0] = pata_gpio_read16_safe(pata, REG_DATA);
for (i = 1; i < words; i++) {
err = __pata_gpio_read16_no_iocfg(pata, REG_DATA, &buf16[i]);
if (err) {
dev_err(ap->dev, "failed to read gpios in %s, code %d\n", __func__, err);
BUG();
}
}
} else {
err = pata_gpio_set_register(pata, REG_DATA);
if (err)
return err;
if (pata->timing)
pata_ndelay(pata->timing->setup);
for (i = 0; i < pata->databus_gpios->ndescs; i++) {
err = gpiod_direction_output(pata->databus_gpios->desc[i], 0);
if (err)
return err;
}
for (i = 0; i < words; i++) {
err = __pata_gpio_write16_no_iocfg(pata, buf16[i]);
if (err)
return err;
}
for (i = 0; i < pata->databus_gpios->ndescs; i++) {
err = gpiod_direction_input(pata->databus_gpios->desc[i]);
if (err)
return err;
}
}
/* Transfer trailing 1 byte, if any. */
if (unlikely(buflen & 0x01)) {
unsigned char *trailing_buf = buf + buflen - 1;
dev_warn(ap->dev, "pata_gpio_data_xfer did uneven length xfer!\n");
if (rw == READ) {
*trailing_buf = pata_gpio_read16_safe(pata, REG_DATA) & 0xFF;
} else {
pata_gpio_write16_safe(pata, REG_DATA, *trailing_buf);
}
}
return buflen;
}
/*
* pata_gpio_set_devctl - Write device control register
*/
static void pata_gpio_set_devctl(struct ata_port *ap, u8 ctl)
{
pata_gpio_write16_safe(ap->host->private_data, REG_CTL, ctl);
}
/*
* pata_gpio_dev_select - Select device on ATA bus
*/
static void pata_gpio_dev_select(struct ata_port *ap, unsigned int device)
{
u8 tmp = ATA_DEVICE_OBS;
if (device != 0)
tmp |= ATA_DEV1;
pata_gpio_write16_safe(ap->host->private_data, REG_DEVICE, tmp);
ata_sff_pause(ap);
}
/*
* pata_gpio_devchk - PATA device presence detection
*/
static bool pata_gpio_devchk(struct ata_port *ap,
unsigned int device)
{
struct pata_gpio *pata = ap->host->private_data;
u8 nsect, lbal;
pata_gpio_dev_select(ap, device);
pata_gpio_write16_safe(pata, REG_NSECT, 0x55);
pata_gpio_write16_safe(pata, REG_LBAL, 0xAA);
pata_gpio_write16_safe(pata, REG_NSECT, 0xAA);
pata_gpio_write16_safe(pata, REG_LBAL, 0x55);
pata_gpio_write16_safe(pata, REG_NSECT, 0x55);
pata_gpio_write16_safe(pata, REG_LBAL, 0xAA);
nsect = pata_gpio_read16_safe(pata, REG_NSECT);
lbal = pata_gpio_read16_safe(pata, REG_LBAL);
return ((nsect == 0x55) && (lbal == 0xaa));
}
/*
* pata_gpio_wait_after_reset - wait for devices to become ready after reset
*/
static int pata_gpio_wait_after_reset(struct ata_link *link,
unsigned long deadline)
{
int rc;
ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
/* always check readiness of the master device */
rc = ata_sff_wait_ready(link, deadline);
/* -ENODEV means the odd clown forgot the D7 pulldown resistor
* and TF status is 0xff, bail out on it too.
*/
if (rc)
return rc;
return 0;
}
/*
* pata_gpio_bus_softreset - PATA device software reset
*/
static int pata_gpio_bus_softreset(struct ata_port *ap,
unsigned long deadline)
{
struct pata_gpio *pata = ap->host->private_data;
/* software reset. causes dev0 to be selected */
pata_gpio_write16_safe(pata, REG_CTL, ap->ctl);
udelay(20);
pata_gpio_write16_safe(pata, REG_CTL, ap->ctl | ATA_SRST);
udelay(20);
pata_gpio_write16_safe(pata, REG_CTL, ap->ctl);
ap->last_ctl = ap->ctl;
return pata_gpio_wait_after_reset(&ap->link, deadline);
}
/*
* pata_gpio_softreset - reset host port via ATA SRST
*/
static int pata_gpio_softreset(struct ata_link *link, unsigned int *classes,
unsigned long deadline)
{
struct ata_port *ap = link->ap;
int rc;
u8 err;
/* issue bus reset */
rc = pata_gpio_bus_softreset(ap, deadline);
if (rc && rc != -ENODEV && rc != -EBUSY) {
ata_link_err(link, "SRST failed (errno=%d)\n", rc);
return rc;
}
/* determine by signature whether we have ATA or ATAPI devices */
classes[0] = ata_sff_dev_classify(&ap->link.device[0], pata_gpio_devchk(ap, 0), &err);
classes[1] = ata_sff_dev_classify(&ap->link.device[1], pata_gpio_devchk(ap, 1), &err);
return 0;
}
static void pata_gpio_drain_fifo(struct ata_queued_cmd *qc)
{
int count;
struct ata_port *ap;
struct pata_gpio *pata;
u16 scratch;
/* We only need to flush incoming data when a command was running */
if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
return;
ap = qc->ap;
pata = ap->host->private_data;
/* Drain up to 64K of data before we give up this recovery method */
if (ap->ops->sff_check_status(ap) & ATA_DRQ) {
pata_gpio_read16_safe(pata, REG_DATA);
for (count = 2; (ap->ops->sff_check_status(ap) & ATA_DRQ) && count < 65536; count += 2)
__pata_gpio_read16_no_iocfg(pata, REG_DATA, &scratch);
}
}
void pata_gpio_set_piomode(struct ata_port *ap, struct ata_device *dev) {
struct pata_gpio *pata = ap->host->private_data;
pata->timing = ata_timing_find_mode(dev->pio_mode);
}
static struct scsi_host_template pata_gpio_sht = {
ATA_PIO_SHT("pata-gpio"),
};
static struct ata_port_operations pata_gpio_port_ops = {
.inherits = &ata_sff_port_ops,
.set_piomode = pata_gpio_set_piomode,
.sff_check_status = pata_gpio_check_status,
.sff_check_altstatus = pata_gpio_check_altstatus,
.sff_tf_load = pata_gpio_tf_load,
.sff_tf_read = pata_gpio_tf_read,
.sff_data_xfer = pata_gpio_data_xfer,
.sff_drain_fifo = pata_gpio_drain_fifo,
.sff_exec_command = pata_gpio_exec_command,
.sff_dev_select = pata_gpio_dev_select,
.sff_set_devctl = pata_gpio_set_devctl,
.softreset = pata_gpio_softreset,
};
static int claim_gpios(struct gpio_descs **target, unsigned count, const char *name, enum gpiod_flags flags, struct device *dev)
{
struct gpio_descs *gpios;
gpios = devm_gpiod_get_array(dev, name, flags);
if (!gpios) {
return -ENOMEM;
}
if (gpios->ndescs != count) {
return -EINVAL;
}
*target = gpios;
return 0;
}
static int pata_gpio_probe(struct platform_device *pdev)
{
int err, irq;
struct device *dev = &pdev->dev;
struct pata_gpio *pata;
struct ata_host *host;
struct ata_port *ap;
pata = devm_kzalloc(dev, sizeof(struct pata_gpio), GFP_KERNEL);
if (!pata)
return -ENOMEM;
pata->dev = dev;
pata->last_reg = REG_INVALID;
err = claim_gpios(&pata->databus_gpios, 16, "databus", GPIOD_IN, dev);
if (err) {
dev_err(dev, "Failed to request databus gpios: %d\n", err);
return err;
}
err = claim_gpios(&pata->cs_gpios, 2, "cs", GPIOD_OUT_LOW, dev);
if (err) {
dev_err(dev, "Failed to request cs gpios: %d\n", err);
return err;
}
err = claim_gpios(&pata->address_gpios, 3, "address", GPIOD_OUT_LOW, dev);
if (err) {
dev_err(dev, "Failed to request address gpios: %d\n", err);
return err;
}
pata->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
if (IS_ERR(pata->reset_gpio))
return PTR_ERR(pata->reset_gpio);
pata->strobe_read_gpio = devm_gpiod_get(dev, "strobe-read", GPIOD_OUT_LOW);
if (!pata->strobe_read_gpio)
return -ENOMEM;
if (IS_ERR(pata->strobe_read_gpio))
return PTR_ERR(pata->strobe_read_gpio);
pata->strobe_write_gpio = devm_gpiod_get(dev, "strobe-write", GPIOD_OUT_LOW);
if (!pata->strobe_write_gpio)
return -ENOMEM;
if (IS_ERR(pata->strobe_write_gpio))
return PTR_ERR(pata->strobe_write_gpio);
// hard-reset
if (pata->reset_gpio) {
gpiod_set_value(pata->reset_gpio, 1);
udelay(20);
gpiod_set_value(pata->reset_gpio, 0);
msleep(100);
}
irq = platform_get_irq_optional(pdev, 0);
if (!irq || irq == -ENXIO) {
dev_warn(dev, "No irq configured, continuing without irq support\n");
irq = 0;
} else if (irq < 0) {
if (irq != -EPROBE_DEFER)
dev_err(dev, "Failed to request irq: %d\n", irq);
return irq;
}
host = ata_host_alloc(&pdev->dev, 1);
if (!host) {
dev_err(dev, "failed to allocate ide host\n");
return -ENOMEM;
}
host->private_data = pata;
ap = host->ports[0];
ap->ops = &pata_gpio_port_ops;
ap->pio_mask = ATA_PIO2; // Limited to PIO2 due to lack of IORDY support
ap->flags = ATA_FLAG_SLAVE_POSS;
if (!irq)
ap->flags |= ATA_FLAG_PIO_POLLING;
return ata_host_activate(host, irq, irq ? ata_sff_interrupt : NULL, 0, &pata_gpio_sht);
}
static const struct of_device_id pata_gpio_dt_ids[] = {
{ .compatible = "pata-gpio" },
{}
};
MODULE_DEVICE_TABLE(of, pata_gpio_dt_ids);
static struct platform_driver pata_gpio_driver = {
.driver = {
.name = "pata-gpio",
.of_match_table = of_match_ptr(pata_gpio_dt_ids),
},
.probe = pata_gpio_probe,
.remove = ata_platform_remove_one,
};
module_platform_driver(pata_gpio_driver);
MODULE_DESCRIPTION("PATA driver using generic bitbanged GPIO");
MODULE_AUTHOR("Tobias Schramm, Tobias Maedel");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");