0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #include <linux/types.h>
0027 #include <linux/errno.h>
0028 #include <linux/kernel.h>
0029 #include <linux/delay.h>
0030 #include <linux/adb.h>
0031 #include <linux/interrupt.h>
0032 #include <linux/init.h>
0033 #include <asm/macintosh.h>
0034 #include <asm/macints.h>
0035 #include <asm/mac_via.h>
0036
0037 static volatile unsigned char *via;
0038
0039
0040 #define RS 0x200
0041 #define B 0
0042 #define A RS
0043 #define DIRB (2*RS)
0044 #define DIRA (3*RS)
0045 #define T1CL (4*RS)
0046 #define T1CH (5*RS)
0047 #define T1LL (6*RS)
0048 #define T1LH (7*RS)
0049 #define T2CL (8*RS)
0050 #define T2CH (9*RS)
0051 #define SR (10*RS)
0052 #define ACR (11*RS)
0053 #define PCR (12*RS)
0054 #define IFR (13*RS)
0055 #define IER (14*RS)
0056 #define ANH (15*RS)
0057
0058
0059 #define CTLR_IRQ 0x08
0060 #define ST_MASK 0x30
0061
0062
0063 #define SR_CTRL 0x1c
0064 #define SR_EXT 0x0c
0065 #define SR_OUT 0x10
0066
0067
0068 #define IER_SET 0x80
0069 #define IER_CLR 0
0070 #define SR_INT 0x04
0071
0072
0073 #define ST_CMD 0x00
0074 #define ST_EVEN 0x10
0075 #define ST_ODD 0x20
0076 #define ST_IDLE 0x30
0077
0078
0079 #define ADDR_MASK 0xF0
0080 #define CMD_MASK 0x0F
0081 #define OP_MASK 0x0C
0082 #define TALK 0x0C
0083
0084 static int macii_init_via(void);
0085 static void macii_start(void);
0086 static irqreturn_t macii_interrupt(int irq, void *arg);
0087 static void macii_queue_poll(void);
0088
0089 static int macii_probe(void);
0090 static int macii_init(void);
0091 static int macii_send_request(struct adb_request *req, int sync);
0092 static int macii_write(struct adb_request *req);
0093 static int macii_autopoll(int devs);
0094 static void macii_poll(void);
0095 static int macii_reset_bus(void);
0096
0097 struct adb_driver via_macii_driver = {
0098 .name = "Mac II",
0099 .probe = macii_probe,
0100 .init = macii_init,
0101 .send_request = macii_send_request,
0102 .autopoll = macii_autopoll,
0103 .poll = macii_poll,
0104 .reset_bus = macii_reset_bus,
0105 };
0106
0107 static enum macii_state {
0108 idle,
0109 sending,
0110 reading,
0111 } macii_state;
0112
0113 static struct adb_request *current_req;
0114 static struct adb_request *last_req;
0115 static unsigned char reply_buf[16];
0116 static unsigned char *reply_ptr;
0117 static bool reading_reply;
0118 static int data_index;
0119 static int reply_len;
0120 static int status;
0121 static bool bus_timeout;
0122 static bool srq_asserted;
0123 static u8 last_cmd;
0124 static u8 last_talk_cmd;
0125 static u8 last_poll_cmd;
0126 static unsigned int autopoll_devs;
0127
0128
0129 static int macii_probe(void)
0130 {
0131 if (macintosh_config->adb_type != MAC_ADB_II)
0132 return -ENODEV;
0133
0134 via = via1;
0135
0136 pr_info("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
0137 return 0;
0138 }
0139
0140
0141 static int macii_init(void)
0142 {
0143 unsigned long flags;
0144 int err;
0145
0146 local_irq_save(flags);
0147
0148 err = macii_init_via();
0149 if (err)
0150 goto out;
0151
0152 err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB",
0153 macii_interrupt);
0154 if (err)
0155 goto out;
0156
0157 macii_state = idle;
0158 out:
0159 local_irq_restore(flags);
0160 return err;
0161 }
0162
0163
0164 static int macii_init_via(void)
0165 {
0166 unsigned char x;
0167
0168
0169 via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;
0170
0171
0172 via[B] |= ST_IDLE;
0173
0174
0175 via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
0176
0177
0178 x = via[SR];
0179
0180 return 0;
0181 }
0182
0183
0184 static void macii_queue_poll(void)
0185 {
0186 static struct adb_request req;
0187 unsigned char poll_command;
0188 unsigned int poll_addr;
0189
0190
0191
0192
0193
0194
0195 if (!autopoll_devs)
0196 return;
0197
0198
0199
0200
0201
0202
0203 poll_addr = (last_poll_cmd & ADDR_MASK) >> 4;
0204 if ((srq_asserted && last_cmd == last_poll_cmd) ||
0205 !(autopoll_devs & (1 << poll_addr))) {
0206 unsigned int higher_devs;
0207
0208 higher_devs = autopoll_devs & -(1 << (poll_addr + 1));
0209 poll_addr = ffs(higher_devs ? higher_devs : autopoll_devs) - 1;
0210 }
0211
0212
0213 poll_command = ADB_READREG(poll_addr, 0);
0214
0215
0216
0217
0218 if (poll_command == last_cmd)
0219 return;
0220
0221 adb_request(&req, NULL, ADBREQ_NOSEND, 1, poll_command);
0222
0223 req.sent = 0;
0224 req.complete = 0;
0225 req.reply_len = 0;
0226 req.next = current_req;
0227
0228 if (WARN_ON(current_req)) {
0229 current_req = &req;
0230 } else {
0231 current_req = &req;
0232 last_req = &req;
0233 }
0234 }
0235
0236
0237 static int macii_send_request(struct adb_request *req, int sync)
0238 {
0239 int err;
0240
0241 err = macii_write(req);
0242 if (err)
0243 return err;
0244
0245 if (sync)
0246 while (!req->complete)
0247 macii_poll();
0248
0249 return 0;
0250 }
0251
0252
0253 static int macii_write(struct adb_request *req)
0254 {
0255 unsigned long flags;
0256
0257 if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
0258 req->complete = 1;
0259 return -EINVAL;
0260 }
0261
0262 req->next = NULL;
0263 req->sent = 0;
0264 req->complete = 0;
0265 req->reply_len = 0;
0266
0267 local_irq_save(flags);
0268
0269 if (current_req != NULL) {
0270 last_req->next = req;
0271 last_req = req;
0272 } else {
0273 current_req = req;
0274 last_req = req;
0275 if (macii_state == idle)
0276 macii_start();
0277 }
0278
0279 local_irq_restore(flags);
0280
0281 return 0;
0282 }
0283
0284
0285 static int macii_autopoll(int devs)
0286 {
0287 unsigned long flags;
0288
0289 local_irq_save(flags);
0290
0291
0292 autopoll_devs = (unsigned int)devs & 0xFFFE;
0293
0294 if (!current_req) {
0295 macii_queue_poll();
0296 if (current_req && macii_state == idle)
0297 macii_start();
0298 }
0299
0300 local_irq_restore(flags);
0301
0302 return 0;
0303 }
0304
0305
0306 static void macii_poll(void)
0307 {
0308 macii_interrupt(0, NULL);
0309 }
0310
0311
0312 static int macii_reset_bus(void)
0313 {
0314 struct adb_request req;
0315
0316
0317 adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET);
0318 macii_send_request(&req, 1);
0319
0320
0321 udelay(3000);
0322
0323 return 0;
0324 }
0325
0326
0327 static void macii_start(void)
0328 {
0329 struct adb_request *req;
0330
0331 req = current_req;
0332
0333
0334
0335
0336
0337
0338
0339 via[ACR] |= SR_OUT;
0340
0341 via[SR] = req->data[1];
0342
0343 via[B] = (via[B] & ~ST_MASK) | ST_CMD;
0344
0345 macii_state = sending;
0346 data_index = 2;
0347
0348 bus_timeout = false;
0349 srq_asserted = false;
0350 }
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370 static irqreturn_t macii_interrupt(int irq, void *arg)
0371 {
0372 int x;
0373 struct adb_request *req;
0374 unsigned long flags;
0375
0376 local_irq_save(flags);
0377
0378 if (!arg) {
0379
0380 if (via[IFR] & SR_INT)
0381 via[IFR] = SR_INT;
0382 else {
0383 local_irq_restore(flags);
0384 return IRQ_NONE;
0385 }
0386 }
0387
0388 status = via[B] & (ST_MASK | CTLR_IRQ);
0389
0390 switch (macii_state) {
0391 case idle:
0392 WARN_ON((status & ST_MASK) != ST_IDLE);
0393
0394 reply_ptr = reply_buf;
0395 reading_reply = false;
0396
0397 bus_timeout = false;
0398 srq_asserted = false;
0399
0400 x = via[SR];
0401
0402 if (!(status & CTLR_IRQ)) {
0403
0404
0405
0406 macii_state = reading;
0407 *reply_ptr = x;
0408 reply_len = 1;
0409 } else {
0410
0411 reply_len = 0;
0412 break;
0413 }
0414
0415
0416 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
0417 break;
0418
0419 case sending:
0420 req = current_req;
0421
0422 if (status == (ST_CMD | CTLR_IRQ)) {
0423
0424
0425
0426
0427
0428 last_cmd = req->data[1];
0429 if ((last_cmd & OP_MASK) == TALK) {
0430 last_talk_cmd = last_cmd;
0431 if ((last_cmd & CMD_MASK) == ADB_READREG(0, 0))
0432 last_poll_cmd = last_cmd;
0433 }
0434 }
0435
0436 if (status == ST_CMD) {
0437
0438
0439
0440
0441 macii_state = reading;
0442
0443 reading_reply = false;
0444 reply_ptr = reply_buf;
0445 *reply_ptr = last_talk_cmd;
0446 reply_len = 1;
0447
0448
0449 via[ACR] &= ~SR_OUT;
0450 x = via[SR];
0451 } else if (data_index >= req->nbytes) {
0452 req->sent = 1;
0453
0454 if (req->reply_expected) {
0455 macii_state = reading;
0456
0457 reading_reply = true;
0458 reply_ptr = req->reply;
0459 *reply_ptr = req->data[1];
0460 reply_len = 1;
0461
0462 via[ACR] &= ~SR_OUT;
0463 x = via[SR];
0464 } else if ((req->data[1] & OP_MASK) == TALK) {
0465 macii_state = reading;
0466
0467 reading_reply = false;
0468 reply_ptr = reply_buf;
0469 *reply_ptr = req->data[1];
0470 reply_len = 1;
0471
0472 via[ACR] &= ~SR_OUT;
0473 x = via[SR];
0474
0475 req->complete = 1;
0476 current_req = req->next;
0477 if (req->done)
0478 (*req->done)(req);
0479 } else {
0480 macii_state = idle;
0481
0482 req->complete = 1;
0483 current_req = req->next;
0484 if (req->done)
0485 (*req->done)(req);
0486 break;
0487 }
0488 } else {
0489 via[SR] = req->data[data_index++];
0490 }
0491
0492 if ((via[B] & ST_MASK) == ST_CMD) {
0493
0494 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
0495 } else {
0496
0497 via[B] ^= ST_MASK;
0498 }
0499 break;
0500
0501 case reading:
0502 x = via[SR];
0503 WARN_ON((status & ST_MASK) == ST_CMD ||
0504 (status & ST_MASK) == ST_IDLE);
0505
0506 if (!(status & CTLR_IRQ)) {
0507 if (status == ST_EVEN && reply_len == 1) {
0508 bus_timeout = true;
0509 } else if (status == ST_ODD && reply_len == 2) {
0510 srq_asserted = true;
0511 } else {
0512 macii_state = idle;
0513
0514 if (bus_timeout)
0515 reply_len = 0;
0516
0517 if (reading_reply) {
0518 struct adb_request *req = current_req;
0519
0520 req->reply_len = reply_len;
0521
0522 req->complete = 1;
0523 current_req = req->next;
0524 if (req->done)
0525 (*req->done)(req);
0526 } else if (reply_len && autopoll_devs &&
0527 reply_buf[0] == last_poll_cmd) {
0528 adb_input(reply_buf, reply_len, 1);
0529 }
0530 break;
0531 }
0532 }
0533
0534 if (reply_len < ARRAY_SIZE(reply_buf)) {
0535 reply_ptr++;
0536 *reply_ptr = x;
0537 reply_len++;
0538 }
0539
0540
0541 via[B] ^= ST_MASK;
0542 break;
0543
0544 default:
0545 break;
0546 }
0547
0548 if (macii_state == idle) {
0549 if (!current_req)
0550 macii_queue_poll();
0551
0552 if (current_req)
0553 macii_start();
0554
0555 if (macii_state == idle) {
0556 via[ACR] &= ~SR_OUT;
0557 x = via[SR];
0558 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
0559 }
0560 }
0561
0562 local_irq_restore(flags);
0563 return IRQ_HANDLED;
0564 }