/*1* Device driver for the via ADB on (many) Mac II-class machines2*3* Based on the original ADB keyboard handler Copyright (c) 1997 Alan Cox4* Also derived from code Copyright (C) 1996 Paul Mackerras.5*6* With various updates provided over the years by Michael Schmitz,7* Guideo Koerber and others.8*9* Rewrite for Unified ADB by Joshua M. Thompson ([email protected])10*11* 1999-08-02 (jmt) - Initial rewrite for Unified ADB.12* 2000-03-29 Tony Mantler <[email protected]>13* - Big overhaul, should actually work now.14* 2006-12-31 Finn Thain <[email protected]> - Another overhaul.15*16* Suggested reading:17* Inside Macintosh, ch. 5 ADB Manager18* Guide to the Macinstosh Family Hardware, ch. 8 Apple Desktop Bus19* Rockwell R6522 VIA datasheet20*21* Apple's "ADB Analyzer" bus sniffer is invaluable:22* ftp://ftp.apple.com/developer/Tool_Chest/Devices_-_Hardware/Apple_Desktop_Bus/23*/2425#include <stdarg.h>26#include <linux/types.h>27#include <linux/errno.h>28#include <linux/kernel.h>29#include <linux/delay.h>30#include <linux/adb.h>31#include <linux/interrupt.h>32#include <linux/init.h>33#include <asm/macintosh.h>34#include <asm/macints.h>35#include <asm/mac_via.h>36#include <asm/system.h>3738static volatile unsigned char *via;3940/* VIA registers - spaced 0x200 bytes apart */41#define RS 0x200 /* skip between registers */42#define B 0 /* B-side data */43#define A RS /* A-side data */44#define DIRB (2*RS) /* B-side direction (1=output) */45#define DIRA (3*RS) /* A-side direction (1=output) */46#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */47#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */48#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */49#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */50#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */51#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */52#define SR (10*RS) /* Shift register */53#define ACR (11*RS) /* Auxiliary control register */54#define PCR (12*RS) /* Peripheral control register */55#define IFR (13*RS) /* Interrupt flag register */56#define IER (14*RS) /* Interrupt enable register */57#define ANH (15*RS) /* A-side data, no handshake */5859/* Bits in B data register: all active low */60#define CTLR_IRQ 0x08 /* Controller rcv status (input) */61#define ST_MASK 0x30 /* mask for selecting ADB state bits */6263/* Bits in ACR */64#define SR_CTRL 0x1c /* Shift register control bits */65#define SR_EXT 0x0c /* Shift on external clock */66#define SR_OUT 0x10 /* Shift out if 1 */6768/* Bits in IFR and IER */69#define IER_SET 0x80 /* set bits in IER */70#define IER_CLR 0 /* clear bits in IER */71#define SR_INT 0x04 /* Shift register full/empty */7273/* ADB transaction states according to GMHW */74#define ST_CMD 0x00 /* ADB state: command byte */75#define ST_EVEN 0x10 /* ADB state: even data byte */76#define ST_ODD 0x20 /* ADB state: odd data byte */77#define ST_IDLE 0x30 /* ADB state: idle, nothing to send */7879static int macii_init_via(void);80static void macii_start(void);81static irqreturn_t macii_interrupt(int irq, void *arg);82static void macii_queue_poll(void);8384static int macii_probe(void);85static int macii_init(void);86static int macii_send_request(struct adb_request *req, int sync);87static int macii_write(struct adb_request *req);88static int macii_autopoll(int devs);89static void macii_poll(void);90static int macii_reset_bus(void);9192struct adb_driver via_macii_driver = {93"Mac II",94macii_probe,95macii_init,96macii_send_request,97macii_autopoll,98macii_poll,99macii_reset_bus100};101102static enum macii_state {103idle,104sending,105reading,106read_done,107} macii_state;108109static struct adb_request *current_req; /* first request struct in the queue */110static struct adb_request *last_req; /* last request struct in the queue */111static unsigned char reply_buf[16]; /* storage for autopolled replies */112static unsigned char *reply_ptr; /* next byte in reply_buf or req->reply */113static int reading_reply; /* store reply in reply_buf else req->reply */114static int data_index; /* index of the next byte to send from req->data */115static int reply_len; /* number of bytes received in reply_buf or req->reply */116static int status; /* VIA's ADB status bits captured upon interrupt */117static int last_status; /* status bits as at previous interrupt */118static int srq_asserted; /* have to poll for the device that asserted it */119static int command_byte; /* the most recent command byte transmitted */120static int autopoll_devs; /* bits set are device addresses to be polled */121122/* Sanity check for request queue. Doesn't check for cycles. */123static int request_is_queued(struct adb_request *req) {124struct adb_request *cur;125unsigned long flags;126local_irq_save(flags);127cur = current_req;128while (cur) {129if (cur == req) {130local_irq_restore(flags);131return 1;132}133cur = cur->next;134}135local_irq_restore(flags);136return 0;137}138139/* Check for MacII style ADB */140static int macii_probe(void)141{142if (macintosh_config->adb_type != MAC_ADB_II) return -ENODEV;143144via = via1;145146printk("adb: Mac II ADB Driver v1.0 for Unified ADB\n");147return 0;148}149150/* Initialize the driver */151int macii_init(void)152{153unsigned long flags;154int err;155156local_irq_save(flags);157158err = macii_init_via();159if (err) goto out;160161err = request_irq(IRQ_MAC_ADB, macii_interrupt, IRQ_FLG_LOCK, "ADB",162macii_interrupt);163if (err) goto out;164165macii_state = idle;166out:167local_irq_restore(flags);168return err;169}170171/* initialize the hardware */172static int macii_init_via(void)173{174unsigned char x;175176/* We want CTLR_IRQ as input and ST_EVEN | ST_ODD as output lines. */177via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;178179/* Set up state: idle */180via[B] |= ST_IDLE;181last_status = via[B] & (ST_MASK|CTLR_IRQ);182183/* Shift register on input */184via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;185186/* Wipe any pending data and int */187x = via[SR];188189return 0;190}191192/* Send an ADB poll (Talk Register 0 command prepended to the request queue) */193static void macii_queue_poll(void)194{195/* No point polling the active device as it will never assert SRQ, so196* poll the next device in the autopoll list. This could leave us197* stuck in a polling loop if an unprobed device is asserting SRQ.198* In theory, that could only happen if a device was plugged in after199* probing started. Unplugging it again will break the cycle.200* (Simply polling the next higher device often ends up polling almost201* every device (after wrapping around), which takes too long.)202*/203int device_mask;204int next_device;205static struct adb_request req;206207if (!autopoll_devs) return;208209device_mask = (1 << (((command_byte & 0xF0) >> 4) + 1)) - 1;210if (autopoll_devs & ~device_mask)211next_device = ffs(autopoll_devs & ~device_mask) - 1;212else213next_device = ffs(autopoll_devs) - 1;214215BUG_ON(request_is_queued(&req));216217adb_request(&req, NULL, ADBREQ_NOSEND, 1,218ADB_READREG(next_device, 0));219220req.sent = 0;221req.complete = 0;222req.reply_len = 0;223req.next = current_req;224225if (current_req != NULL) {226current_req = &req;227} else {228current_req = &req;229last_req = &req;230}231}232233/* Send an ADB request; if sync, poll out the reply 'till it's done */234static int macii_send_request(struct adb_request *req, int sync)235{236int err;237unsigned long flags;238239BUG_ON(request_is_queued(req));240241local_irq_save(flags);242err = macii_write(req);243local_irq_restore(flags);244245if (!err && sync) {246while (!req->complete) {247macii_poll();248}249BUG_ON(request_is_queued(req));250}251252return err;253}254255/* Send an ADB request (append to request queue) */256static int macii_write(struct adb_request *req)257{258if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {259req->complete = 1;260return -EINVAL;261}262263req->next = NULL;264req->sent = 0;265req->complete = 0;266req->reply_len = 0;267268if (current_req != NULL) {269last_req->next = req;270last_req = req;271} else {272current_req = req;273last_req = req;274if (macii_state == idle) macii_start();275}276return 0;277}278279/* Start auto-polling */280static int macii_autopoll(int devs)281{282static struct adb_request req;283unsigned long flags;284int err = 0;285286/* bit 1 == device 1, and so on. */287autopoll_devs = devs & 0xFFFE;288289if (!autopoll_devs) return 0;290291local_irq_save(flags);292293if (current_req == NULL) {294/* Send a Talk Reg 0. The controller will repeatedly transmit295* this as long as it is idle.296*/297adb_request(&req, NULL, ADBREQ_NOSEND, 1,298ADB_READREG(ffs(autopoll_devs) - 1, 0));299err = macii_write(&req);300}301302local_irq_restore(flags);303return err;304}305306static inline int need_autopoll(void) {307/* Was the last command Talk Reg 0308* and is the target on the autopoll list?309*/310if ((command_byte & 0x0F) == 0x0C &&311((1 << ((command_byte & 0xF0) >> 4)) & autopoll_devs))312return 0;313return 1;314}315316/* Prod the chip without interrupts */317static void macii_poll(void)318{319disable_irq(IRQ_MAC_ADB);320macii_interrupt(0, NULL);321enable_irq(IRQ_MAC_ADB);322}323324/* Reset the bus */325static int macii_reset_bus(void)326{327static struct adb_request req;328329if (request_is_queued(&req))330return 0;331332/* Command = 0, Address = ignored */333adb_request(&req, NULL, 0, 1, ADB_BUSRESET);334335/* Don't want any more requests during the Global Reset low time. */336udelay(3000);337338return 0;339}340341/* Start sending ADB packet */342static void macii_start(void)343{344struct adb_request *req;345346req = current_req;347348BUG_ON(req == NULL);349350BUG_ON(macii_state != idle);351352/* Now send it. Be careful though, that first byte of the request353* is actually ADB_PACKET; the real data begins at index 1!354* And req->nbytes is the number of bytes of real data plus one.355*/356357/* store command byte */358command_byte = req->data[1];359/* Output mode */360via[ACR] |= SR_OUT;361/* Load data */362via[SR] = req->data[1];363/* set ADB state to 'command' */364via[B] = (via[B] & ~ST_MASK) | ST_CMD;365366macii_state = sending;367data_index = 2;368}369370/*371* The notorious ADB interrupt handler - does all of the protocol handling.372* Relies on the ADB controller sending and receiving data, thereby373* generating shift register interrupts (SR_INT) for us. This means there has374* to be activity on the ADB bus. The chip will poll to achieve this.375*376* The basic ADB state machine was left unchanged from the original MacII code377* by Alan Cox, which was based on the CUDA driver for PowerMac.378* The syntax of the ADB status lines is totally different on MacII,379* though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle380* for sending and Idle -> Even -> Odd -> Even ->...-> Idle for receiving.381* Start and end of a receive packet are signalled by asserting /IRQ on the382* interrupt line (/IRQ means the CTLR_IRQ bit in port B; not to be confused383* with the VIA shift register interrupt. /IRQ never actually interrupts the384* processor, it's just an ordinary input.)385*/386static irqreturn_t macii_interrupt(int irq, void *arg)387{388int x;389static int entered;390struct adb_request *req;391392if (!arg) {393/* Clear the SR IRQ flag when polling. */394if (via[IFR] & SR_INT)395via[IFR] = SR_INT;396else397return IRQ_NONE;398}399400BUG_ON(entered++);401402last_status = status;403status = via[B] & (ST_MASK|CTLR_IRQ);404405switch (macii_state) {406case idle:407if (reading_reply) {408reply_ptr = current_req->reply;409} else {410BUG_ON(current_req != NULL);411reply_ptr = reply_buf;412}413414x = via[SR];415416if ((status & CTLR_IRQ) && (x == 0xFF)) {417/* Bus timeout without SRQ sequence:418* data is "FF" while CTLR_IRQ is "H"419*/420reply_len = 0;421srq_asserted = 0;422macii_state = read_done;423} else {424macii_state = reading;425*reply_ptr = x;426reply_len = 1;427}428429/* set ADB state = even for first data byte */430via[B] = (via[B] & ~ST_MASK) | ST_EVEN;431break;432433case sending:434req = current_req;435if (data_index >= req->nbytes) {436req->sent = 1;437macii_state = idle;438439if (req->reply_expected) {440reading_reply = 1;441} else {442req->complete = 1;443current_req = req->next;444if (req->done) (*req->done)(req);445446if (current_req)447macii_start();448else449if (need_autopoll())450macii_autopoll(autopoll_devs);451}452453if (macii_state == idle) {454/* reset to shift in */455via[ACR] &= ~SR_OUT;456x = via[SR];457/* set ADB state idle - might get SRQ */458via[B] = (via[B] & ~ST_MASK) | ST_IDLE;459}460} else {461via[SR] = req->data[data_index++];462463if ( (via[B] & ST_MASK) == ST_CMD ) {464/* just sent the command byte, set to EVEN */465via[B] = (via[B] & ~ST_MASK) | ST_EVEN;466} else {467/* invert state bits, toggle ODD/EVEN */468via[B] ^= ST_MASK;469}470}471break;472473case reading:474x = via[SR];475BUG_ON((status & ST_MASK) == ST_CMD ||476(status & ST_MASK) == ST_IDLE);477478/* Bus timeout with SRQ sequence:479* data is "XX FF" while CTLR_IRQ is "L L"480* End of packet without SRQ sequence:481* data is "XX...YY 00" while CTLR_IRQ is "L...H L"482* End of packet SRQ sequence:483* data is "XX...YY 00" while CTLR_IRQ is "L...L L"484* (where XX is the first response byte and485* YY is the last byte of valid response data.)486*/487488srq_asserted = 0;489if (!(status & CTLR_IRQ)) {490if (x == 0xFF) {491if (!(last_status & CTLR_IRQ)) {492macii_state = read_done;493reply_len = 0;494srq_asserted = 1;495}496} else if (x == 0x00) {497macii_state = read_done;498if (!(last_status & CTLR_IRQ))499srq_asserted = 1;500}501}502503if (macii_state == reading) {504BUG_ON(reply_len > 15);505reply_ptr++;506*reply_ptr = x;507reply_len++;508}509510/* invert state bits, toggle ODD/EVEN */511via[B] ^= ST_MASK;512break;513514case read_done:515x = via[SR];516517if (reading_reply) {518reading_reply = 0;519req = current_req;520req->reply_len = reply_len;521req->complete = 1;522current_req = req->next;523if (req->done) (*req->done)(req);524} else if (reply_len && autopoll_devs)525adb_input(reply_buf, reply_len, 0);526527macii_state = idle;528529/* SRQ seen before, initiate poll now */530if (srq_asserted)531macii_queue_poll();532533if (current_req)534macii_start();535else536if (need_autopoll())537macii_autopoll(autopoll_devs);538539if (macii_state == idle)540via[B] = (via[B] & ~ST_MASK) | ST_IDLE;541break;542543default:544break;545}546547entered--;548return IRQ_HANDLED;549}550551552