| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918 |
- /* mediatek/kernel/drivers/uart/uart.c
- *
- * (C) Copyright 2008
- * MediaTek <www.mediatek.com>
- * MingHsien Hsieh <minghsien.hsieh@mediatek.com>
- *
- * MTK UART Driver
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- /******************************************************************************
- * Dependency
- ******************************************************************************/
- #if defined(CONFIG_MTK_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
- #define SUPPORT_SYSRQ /*used in serial_core.h */
- #endif
- /*---------------------------------------------------------------------------*/
- #include <generated/autoconf.h>
- #include <linux/platform_device.h>
- #include <linux/module.h>
- #include <linux/ioport.h>
- #include <linux/init.h>
- #include <linux/console.h>
- #include <linux/sysrq.h>
- #include <linux/device.h>
- #include <linux/tty.h>
- #include <linux/tty_flip.h>
- #include <linux/serial_core.h>
- #include <linux/serial.h>
- #include <linux/timer.h>
- #include <linux/dma-mapping.h>
- #include <linux/scatterlist.h>
- #include <linux/platform_device.h>
- #include <linux/hrtimer.h>
- #include <linux/uaccess.h>
- #include <asm/atomic.h>
- #include <asm/io.h>
- #include <asm/irq.h>
- #include <linux/irq.h>
- #include <asm/scatterlist.h>
- #include <mt-plat/dma.h>
- /* #include <mach/mt_clkmgr.h> */
- #include <linux/slab.h>
- #include <linux/delay.h>
- #include <linux/syscore_ops.h>
- #include "include/mtk_uart.h"
- #include "include/mtk_uart_intf.h"
- #include <linux/version.h>
- #include <linux/suspend.h>
- #ifdef CONFIG_OF
- #include <linux/of.h>
- #endif
- #include "include/mtk_uart_internal.h"
- #define TTY_FLIP_ARG(a) ((a)->port)
- spinlock_t mtk_console_lock;
- spinlock_t mtk_uart_bt_lock;
- struct mtk_uart *console_port;
- struct mtk_uart *bt_port = NULL;
- /*---------------------------------------------------------------------------*/
- #define HW_FLOW_CTRL_PORT(uart) (uart->setting->hw_flow)
- /*---------------------------------------------------------------------------*/
- #if defined(ENABLE_VFIFO)
- /*---------------------------------------------------------------------------*/
- static DEFINE_SPINLOCK(mtk_uart_vfifo_port_lock);
- /*---------------------------------------------------------------------------*/
- #ifdef CONFIG_OF
- /*---------------------------------------------------------------------------*/
- #define VFIFO_INIT_RX(c, i, n) \
- {.ch = (c), .size = (n), .trig = VFF_RX_THRE(n), .type = UART_RX_VFIFO, \
- .port = NULL, .addr = NULL, \
- .entry = ATOMIC_INIT(0), .reg_cb = ATOMIC_INIT(0), \
- .iolock = __SPIN_LOCK_UNLOCKED(mtk_uart_vfifo_port[i].lock)}
- /*---------------------------------------------------------------------------*/
- #define VFIFO_INIT_TX(c, i, n) \
- {.ch = (c), .size = (n), .trig = VFF_TX_THRE(n), .type = UART_TX_VFIFO, \
- .port = NULL, \
- .addr = NULL, .entry = ATOMIC_INIT(0), .reg_cb = ATOMIC_INIT(0), \
- .iolock = __SPIN_LOCK_UNLOCKED(mtk_uart_vfifo_port[i].lock)}
- /*---------------------------------------------------------------------------*/
- static struct mtk_uart_vfifo mtk_uart_vfifo_port[] = {
- VFIFO_INIT_TX(P_DMA_UART1_TX, 0, C_UART1_VFF_TX_SIZE),
- VFIFO_INIT_RX(P_DMA_UART1_RX, 1, C_UART1_VFF_RX_SIZE),
- VFIFO_INIT_TX(P_DMA_UART2_TX, 2, C_UART2_VFF_TX_SIZE),
- VFIFO_INIT_RX(P_DMA_UART2_RX, 3, C_UART2_VFF_RX_SIZE),
- VFIFO_INIT_TX(P_DMA_UART3_TX, 4, C_UART3_VFF_TX_SIZE),
- VFIFO_INIT_RX(P_DMA_UART3_RX, 5, C_UART3_VFF_RX_SIZE),
- #if 0 /*MT6589 only 6 DMA channel for UART */
- VFIFO_INIT_TX(P_DMA_UART4_TX, 6, C_UART4_VFF_TX_SIZE),
- VFIFO_INIT_RX(P_DMA_UART4_RX, 7, C_UART4_VFF_RX_SIZE),
- #endif
- };
- /*---------------------------------------------------------------------------*/
- #else
- /*---------------------------------------------------------------------------*/
- #define VFIFO_INIT_RX(c, i, n, id) \
- {.ch = (c), .size = (n), .trig = VFF_RX_THRE(n), .type = UART_RX_VFIFO, \
- .base = (void *)VFF_BASE_CH(i), .port = NULL, .addr = NULL, \
- .entry = ATOMIC_INIT(0), .reg_cb = ATOMIC_INIT(0), \
- .iolock = __SPIN_LOCK_UNLOCKED(mtk_uart_vfifo_port[i].lock), \
- .irq_id = id}
- /*---------------------------------------------------------------------------*/
- #define VFIFO_INIT_TX(c, i, n, id) \
- {.ch = (c), .size = (n), .trig = VFF_TX_THRE(n), .type = UART_TX_VFIFO, \
- .base = (void *)VFF_BASE_CH(i), .port = NULL, \
- .addr = NULL, .entry = ATOMIC_INIT(0), .reg_cb = ATOMIC_INIT(0), \
- .iolock = __SPIN_LOCK_UNLOCKED(mtk_uart_vfifo_port[i].lock), \
- .irq_id = id}
- /*---------------------------------------------------------------------------*/
- static struct mtk_uart_vfifo mtk_uart_vfifo_port[] = {
- VFIFO_INIT_TX(P_DMA_UART1_TX, 0, C_UART1_VFF_TX_SIZE, UART1_VFF_TX_IRQ_ID),
- VFIFO_INIT_RX(P_DMA_UART1_RX, 1, C_UART1_VFF_RX_SIZE, UART1_VFF_RX_IRQ_ID),
- VFIFO_INIT_TX(P_DMA_UART2_TX, 2, C_UART2_VFF_TX_SIZE, UART2_VFF_TX_IRQ_ID),
- VFIFO_INIT_RX(P_DMA_UART2_RX, 3, C_UART2_VFF_RX_SIZE, UART2_VFF_RX_IRQ_ID),
- VFIFO_INIT_TX(P_DMA_UART3_TX, 4, C_UART3_VFF_TX_SIZE, UART3_VFF_TX_IRQ_ID),
- VFIFO_INIT_RX(P_DMA_UART3_RX, 5, C_UART3_VFF_RX_SIZE, UART3_VFF_RX_IRQ_ID),
- #if 0 /*MT6589 only 6 DMA channel for UART */
- VFIFO_INIT_TX(P_DMA_UART4_TX, 6, C_UART4_VFF_TX_SIZE),
- VFIFO_INIT_RX(P_DMA_UART4_RX, 7, C_UART4_VFF_RX_SIZE),
- #endif
- };
- /*---------------------------------------------------------------------------*/
- #endif /*CONFIG_OF */
- /*---------------------------------------------------------------------------*/
- #endif /*ENABLE_VFIFO */
- /*---------------------------------------------------------------------------*/
- /* uart control blocks */
- static struct mtk_uart mtk_uarts[UART_NR];
- static unsigned int uart_freeze_enable[UART_NR] = { 0 };
- struct uart_history_data {
- int index;
- int offset;
- u64 last_update;
- unsigned char *buffer;
- };
- #define UART_HISTORY_DATA_SIZE 4096
- static struct uart_history_data tx_history;
- static struct uart_history_data rx_history;
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_init_ports(void);
- static void mtk_uart_stop_tx(struct uart_port *port);
- /******************************************************************************
- * SYSFS support
- ******************************************************************************/
- #if defined(ENABLE_SYSFS)
- /*---------------------------------------------------------------------------*/
- const struct sysfs_ops mtk_uart_sysfs_ops = {
- .show = mtk_uart_attr_show,
- .store = mtk_uart_attr_store,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry {
- struct attribute attr;
- ssize_t (*show)(struct kobject *kobj, char *page);
- ssize_t (*store)(struct kobject *kobj, const char *page, size_t size);
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry debug_entry = {
- {.name = "debug", .mode = S_IRUGO | S_IWUSR},
- mtk_uart_debug_show,
- mtk_uart_debug_store,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry sysrq_entry = {
- {.name = "sysrq", .mode = S_IRUGO | S_IWUSR},
- mtk_uart_sysrq_show,
- mtk_uart_sysrq_store,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry vffsz_entry = {
- {.name = "vffsz", .mode = S_IRUGO | S_IWUSR},
- mtk_uart_vffsz_show,
- mtk_uart_vffsz_store,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry conse_entry = {
- {.name = "conse", .mode = S_IRUGO | S_IWUSR},
- mtk_uart_conse_show,
- mtk_uart_conse_store,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry vff_en_entry = {
- {.name = "vff_en", .mode = S_IRUGO | S_IWUSR},
- mtk_uart_vff_en_show,
- mtk_uart_vff_en_store,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry lsr_status_entry = {
- {.name = "lsr_status", .mode = S_IRUGO | S_IWUSR},
- mtk_uart_lsr_status_show,
- mtk_uart_lsr_status_store,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_entry history_entry = {
- {.name = "history", .mode = S_IRUGO | S_IWUSR},
- mtk_uart_history_show,
- mtk_uart_history_store,
- };
- /*---------------------------------------------------------------------------*/
- struct attribute *mtk_uart_attributes[] = {
- &conse_entry.attr, /*console setting */
- #if defined(ENABLE_DEBUG)
- &debug_entry.attr,
- &sysrq_entry.attr,
- &vffsz_entry.attr,
- &vff_en_entry.attr,
- &lsr_status_entry.attr,
- &history_entry.attr,
- #endif
- NULL,
- };
- /*---------------------------------------------------------------------------*/
- struct kobj_type mtk_uart_ktype = {
- .sysfs_ops = &mtk_uart_sysfs_ops,
- .default_attrs = mtk_uart_attributes,
- };
- /*---------------------------------------------------------------------------*/
- struct mtuart_sysobj {
- struct kobject kobj;
- atomic_t sysrq;
- atomic_t vffLen[UART_NR * UART_VFIFO_NUM];
- atomic_t console_enable;
- } mtk_uart_sysobj = {
- .console_enable = ATOMIC_INIT(1),};
- /*---------------------------------------------------------------------------*/
- int mtk_uart_sysfs(void)
- {
- struct mtuart_sysobj *obj = &mtk_uart_sysobj;
- #if defined(ENABLE_VFIFO)
- int idx;
- #endif
- memset(&obj->kobj, 0x00, sizeof(obj->kobj));
- #if defined(CONFIG_MAGIC_SYSRQ)
- atomic_set(&obj->sysrq, 1);
- #else
- atomic_set(&obj->sysrq, 0);
- #endif
- #if defined(ENABLE_VFIFO)
- for (idx = 0; idx < ARRAY_SIZE(obj->vffLen) && idx < ARRAY_SIZE(mtk_uart_vfifo_port); idx++)
- atomic_set(&obj->vffLen[idx], mtk_uart_vfifo_port[idx].size);
- #endif
- atomic_set(&obj->console_enable, 1);
- obj->kobj.parent = kernel_kobj;
- if (kobject_init_and_add(&obj->kobj, &mtk_uart_ktype, NULL, "mtuart")) {
- kobject_put(&obj->kobj);
- return -ENOMEM;
- }
- kobject_uevent(&obj->kobj, KOBJ_ADD);
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_attr_show(struct kobject *kobj, struct attribute *attr, char *buffer)
- {
- struct mtuart_entry *entry = container_of(attr, struct mtuart_entry, attr);
- return entry->show(kobj, buffer);
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_attr_store(struct kobject *kobj, struct attribute *attr, const char *buffer, size_t size)
- {
- struct mtuart_entry *entry = container_of(attr, struct mtuart_entry, attr);
- return entry->store(kobj, buffer, size);
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_debug_show(struct kobject *kobj, char *buffer)
- {
- int remain = PAGE_SIZE;
- int len;
- char *ptr = buffer;
- int idx;
- unsigned int evt_mask;
- for (idx = 0; idx < UART_NR; idx++) {
- evt_mask = (unsigned int)get_uart_evt_mask(idx);
- len = scnprintf(ptr, remain, "0x%2x\n", evt_mask);
- ptr += len;
- remain -= len;
- }
- return PAGE_SIZE - remain;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_debug_store(struct kobject *kobj, const char *buffer, size_t size)
- {
- #if (UART_NR < 5)
- int a, b, c, d;
- int res = sscanf(buffer, "0x%x 0x%x 0x%x 0x%x", &a, &b, &c, &d);
- if (res != 4) {
- MSG_ERR("%s: expect 4 numbers\n", __func__);
- } else {
- set_uart_evt_mask(0, a);
- set_uart_evt_mask(1, b);
- set_uart_evt_mask(2, c);
- set_uart_evt_mask(3, d);
- }
- #else
- int a, b, c, d, e;
- int res = sscanf(buffer, "0x%x 0x%x 0x%x 0x%x 0x%x", &a, &b, &c, &d, &e);
- if (res != 5) {
- MSG_ERR("%s: expect 5 numbers\n", __func__);
- } else {
- set_uart_evt_mask(0, a);
- set_uart_evt_mask(1, b);
- set_uart_evt_mask(2, c);
- set_uart_evt_mask(3, d);
- set_uart_evt_mask(4, e);
- }
- #endif
- return size;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_sysrq_show(struct kobject *kobj, char *buffer)
- {
- struct mtuart_sysobj *obj = container_of(kobj, struct mtuart_sysobj, kobj);
- return scnprintf(buffer, PAGE_SIZE, "%d\n", atomic_read(&obj->sysrq));
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_sysrq_store(struct kobject *kobj, const char *buffer, size_t size)
- {
- struct mtuart_sysobj *obj = container_of(kobj, struct mtuart_sysobj, kobj);
- int a;
- int res = sscanf(buffer, "%d\n", &a);
- if (res != 1)
- MSG_ERR("%s: expect 1 number\n", __func__);
- else
- atomic_set(&obj->sysrq, a);
- return size;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_vffsz_show(struct kobject *kobj, char *buffer)
- {
- ssize_t len = 0;
- #if defined(ENABLE_VFIFO)
- struct mtuart_sysobj *obj = container_of(kobj, struct mtuart_sysobj, kobj);
- int idx;
- for (idx = 0; idx < ARRAY_SIZE(obj->vffLen); idx++)
- len += scnprintf(buffer + len, PAGE_SIZE - len, "[%02d] %4d\n", idx, atomic_read(&obj->vffLen[idx]));
- #endif
- return len;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_vffsz_store(struct kobject *kobj, const char *buffer, size_t size)
- {
- #if defined(ENABLE_VFIFO)
- struct mtuart_sysobj *obj = container_of(kobj, struct mtuart_sysobj, kobj);
- int idx, sz;
- if (2 != sscanf(buffer, "%d %d", &idx, &sz))
- MSG_ERR("%s: expect 2 variables\n", __func__);
- else if (idx >= ARRAY_SIZE(obj->vffLen) || (sz % 8 != 0))
- MSG_ERR("%s: invalid args %d, %d\n", __func__, idx, sz);
- else
- atomic_set(&obj->vffLen[idx], sz);
- #endif
- return size;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_conse_show(struct kobject *kobj, char *buffer)
- {
- struct mtuart_sysobj *obj = container_of(kobj, struct mtuart_sysobj, kobj);
- return scnprintf(buffer, PAGE_SIZE, "%d\n", atomic_read(&obj->console_enable));
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_conse_store(struct kobject *kobj, const char *buffer, size_t size)
- {
- struct mtuart_sysobj *obj = container_of(kobj, struct mtuart_sysobj, kobj);
- int enable;
- if (kstrtoint(buffer, 10, &enable))
- MSG_ERR("%s: expect 1 variables\n", __func__);
- else
- atomic_set(&obj->console_enable, enable);
- return size;
- }
- /*---------------------------------------------------------------------------*/
- #endif /*ENABLE_SYSFS */
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_vff_en_show(struct kobject *kobj, char *buffer)
- {
- int remain = PAGE_SIZE;
- int len;
- char *ptr = buffer;
- int idx;
- struct mtk_uart_setting *uart_setting;
- for (idx = 0; idx < UART_NR; idx++) {
- uart_setting = get_uart_default_settings(idx);
- len = scnprintf(ptr,
- remain,
- "tx%d_m:%2x rx%d_m:%2x\n",
- idx, (unsigned int)uart_setting->tx_mode, idx, (unsigned int)uart_setting->rx_mode);
- ptr += len;
- remain -= len;
- }
- return PAGE_SIZE - remain;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_vff_en_store(struct kobject *kobj, const char *buffer, size_t size)
- {
- #if (UART_NR < 5)
- int u1_tx, u1_rx, u2_tx, u2_rx, u3_tx, u3_rx, u4_tx, u4_rx;
- struct mtk_uart_setting *uart_setting;
- int res = sscanf(buffer, "%x %x %x %x %x %x %x %x",
- &u1_tx, &u1_rx, &u2_tx, &u2_rx, &u3_tx, &u3_rx, &u4_tx, &u4_rx);
- if (res != 8) {
- MSG_ERR("%s: expect 8 numbers\n", __func__);
- } else {
- uart_setting = get_uart_default_settings(0);
- uart_setting->tx_mode = u1_tx;
- uart_setting->rx_mode = u1_rx;
- uart_setting = get_uart_default_settings(1);
- uart_setting->tx_mode = u2_tx;
- uart_setting->rx_mode = u2_rx;
- uart_setting = get_uart_default_settings(2);
- uart_setting->tx_mode = u3_tx;
- uart_setting->rx_mode = u3_rx;
- uart_setting = get_uart_default_settings(3);
- uart_setting->tx_mode = u4_tx;
- uart_setting->rx_mode = u4_rx;
- }
- return size;
- #else
- int u1_tx, u1_rx, u2_tx, u2_rx, u3_tx, u3_rx, u4_tx, u4_rx, u5_tx, u5_rx;
- struct mtk_uart_setting *uart_setting;
- int res = sscanf(buffer, "%x %x %x %x %x %x %x %x %x %x",
- &u1_tx, &u1_rx, &u2_tx, &u2_rx, &u3_tx, &u3_rx, &u4_tx, &u4_rx, &u5_tx, &u5_rx);
- if (res != 8) {
- MSG_ERR("%s: expect 8 numbers\n", __func__);
- } else {
- uart_setting = get_uart_default_settings(0);
- uart_setting->tx_mode = u1_tx;
- uart_setting->rx_mode = u1_rx;
- uart_setting = get_uart_default_settings(1);
- uart_setting->tx_mode = u2_tx;
- uart_setting->rx_mode = u2_rx;
- uart_setting = get_uart_default_settings(2);
- uart_setting->tx_mode = u3_tx;
- uart_setting->rx_mode = u3_rx;
- uart_setting = get_uart_default_settings(3);
- uart_setting->tx_mode = u4_tx;
- uart_setting->rx_mode = u4_rx;
- uart_setting = get_uart_default_settings(4);
- uart_setting->tx_mode = u5_tx;
- uart_setting->rx_mode = u5_rx;
- }
- return size;
- #endif
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_lsr_status_show(struct kobject *kobj, char *buffer)
- {
- int remain = PAGE_SIZE;
- int len;
- char *ptr = buffer;
- int idx;
- unsigned long lsr_status;
- for (idx = 0; idx < UART_NR; idx++) {
- lsr_status = get_uart_lsr_status(idx);
- len = scnprintf(ptr, remain, "%04x ", (unsigned int)lsr_status);
- ptr += len;
- remain -= len;
- set_uart_lsr_status(idx, 0);
- }
- len = scnprintf(ptr, remain, "\n");
- ptr += len;
- remain -= len;
- return PAGE_SIZE - remain;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_lsr_status_store(struct kobject *kobj, const char *buffer, size_t size)
- {
- #if (UART_NR < 5)
- int u1_lsr, u2_lsr, u3_lsr, u4_lsr;
- int res = sscanf(buffer, "%x %x %x %x",
- &u1_lsr, &u2_lsr, &u3_lsr, &u4_lsr);
- if (res != 4) {
- MSG_ERR("%s: expect 4 numbers\n", __func__);
- } else {
- set_uart_lsr_status(0, u1_lsr);
- set_uart_lsr_status(1, u2_lsr);
- set_uart_lsr_status(2, u3_lsr);
- set_uart_lsr_status(3, u4_lsr);
- }
- return size;
- #else
- int u1_lsr, u2_lsr, u3_lsr, u4_lsr, u5_lsr;
- int res = sscanf(buffer, "%x %x %x %x %x",
- &u1_lsr, &u2_lsr, &u3_lsr, &u4_lsr, &u5_lsr);
- if (res != 5) {
- MSG_ERR("%s: expect 5 numbers\n", __func__);
- } else {
- set_uart_lsr_status(0, u1_lsr);
- set_uart_lsr_status(1, u2_lsr);
- set_uart_lsr_status(2, u3_lsr);
- set_uart_lsr_status(3, u4_lsr);
- set_uart_lsr_status(4, u4_lsr);
- }
- return size;
- #endif
- }
- /*---------------------------------------------------------------------------*/
- static void uart_mem_dump(int nport, void *start_addr, int len)
- {
- unsigned int *curr_p = (unsigned int *)start_addr;
- unsigned char *curr_ch_p;
- int _16_fix_num = len / 16;
- int tail_num = len % 16;
- char buf[16];
- int i, j;
- if (NULL == curr_p) {
- pr_err("[UART%d-DUMP]NULL point to dump!\n", nport);
- return;
- }
- if (0 == len) {
- pr_err("[UART%d-DUMP]Not need to dump\n", nport);
- return;
- }
- pr_debug("[UART%d-DUMP]Base: %p\n", nport, start_addr);
- /* Fix section */
- for (i = 0; i < _16_fix_num; i++) {
- pr_debug("[UART%d-DUMP]%03X: %08X %08X %08X %08X\n",
- nport, i * 16, *curr_p, *(curr_p + 1), *(curr_p + 2), *(curr_p + 3));
- curr_p += 4;
- }
- /* Tail section */
- if (tail_num > 0) {
- curr_ch_p = (unsigned char *)curr_p;
- for (j = 0; j < tail_num; j++) {
- buf[j] = *curr_ch_p;
- curr_ch_p++;
- }
- for (; j < 16; j++)
- buf[j] = 0;
- curr_p = (unsigned int *)buf;
- pr_debug("[UART%d-DUMP]%03X: %08X %08X %08X %08X\n",
- nport, i * 16, *curr_p, *(curr_p + 1), *(curr_p + 2), *(curr_p + 3));
- }
- }
- void mtk_uart_dump_history(void)
- {
- u64 ts_nsec;
- unsigned long rem_nsec;
- ts_nsec = tx_history.last_update;
- rem_nsec = do_div(ts_nsec, 1000000000);
- pr_debug("UART Tx port %d: %d/%d @[%5lu.%06lu]\n",
- tx_history.index, tx_history.offset, UART_HISTORY_DATA_SIZE, (unsigned long)ts_nsec, rem_nsec / 1000);
- uart_mem_dump(tx_history.index, tx_history.buffer, UART_HISTORY_DATA_SIZE);
- ts_nsec = rx_history.last_update;
- rem_nsec = do_div(ts_nsec, 1000000000);
- pr_debug("UART Rx port %d: %d/%d @[%5lu.%06lu]\n",
- rx_history.index, rx_history.offset, UART_HISTORY_DATA_SIZE, (unsigned long)ts_nsec, rem_nsec / 1000);
- uart_mem_dump(rx_history.index, rx_history.buffer, UART_HISTORY_DATA_SIZE);
- }
- void update_history_byte(char is_tx, int nport, unsigned char byte)
- {
- struct uart_history_data *x_history;
- x_history = is_tx ? &tx_history : &rx_history;
- if (nport == x_history->index) {
- *(x_history->buffer + x_history->offset) = byte;
- x_history->offset++;
- if (x_history->offset == UART_HISTORY_DATA_SIZE)
- x_history->offset = 0;
- }
- }
- void update_history_time(char is_tx, int nport)
- {
- struct uart_history_data *x_history;
- x_history = is_tx ? &tx_history : &rx_history;
- if (nport == x_history->index)
- x_history->last_update = local_clock();
- }
- void update_history_bulk(char is_tx, int nport, unsigned char *chars, int count)
- {
- int room;
- struct uart_history_data *x_history;
- x_history = is_tx ? &tx_history : &rx_history;
- room = UART_HISTORY_DATA_SIZE - x_history->offset;
- if (nport == x_history->index) {
- if (count <= room) {
- memcpy(x_history->buffer + x_history->offset, chars, count);
- x_history->offset += count;
- if (x_history->offset == UART_HISTORY_DATA_SIZE)
- x_history->offset = 0;
- } else {
- memcpy(x_history->buffer + x_history->offset, chars, room);
- memcpy(x_history->buffer, chars, count - room);
- x_history->offset = count - room;
- if (x_history->offset == UART_HISTORY_DATA_SIZE)
- x_history->offset = 0;
- }
- }
- x_history->last_update = local_clock();
- }
- ssize_t mtk_uart_history_show(struct kobject *kobj, char *buffer)
- {
- int remain = PAGE_SIZE;
- int len;
- char *ptr = buffer;
- len = scnprintf(ptr, remain, "tx(%d):%d; rx(%d):%d\n",
- tx_history.index, tx_history.offset, rx_history.index, rx_history.offset);
- ptr += len;
- remain -= len;
- mtk_uart_dump_history();
- return PAGE_SIZE - remain;
- }
- /*---------------------------------------------------------------------------*/
- ssize_t mtk_uart_history_store(struct kobject *kobj, const char *buffer, size_t size)
- {
- int tx_index, rx_index;
- int res = sscanf(buffer, "%d %d", &tx_index, &rx_index);
- if (res != 2)
- return 0;
- tx_history.index = tx_index;
- rx_history.index = rx_index;
- tx_history.offset = 0;
- tx_history.offset = 0;
- memset(tx_history.buffer, 0, UART_HISTORY_DATA_SIZE);
- memset(rx_history.buffer, 0, UART_HISTORY_DATA_SIZE);
- return size;
- }
- /* ================================ FIQ ========================================== */
- #if (defined(CONFIG_FIQ_DEBUGGER_CONSOLE) && defined(CONFIG_FIQ_DEBUGGER))
- #define DEFAULT_FIQ_UART_PORT (3)
- int fiq_console_port = DEFAULT_FIQ_UART_PORT;
- /* struct uart_port *p_mtk_uart_port = &(mtk_uarts[DEFAULT_FIQ_UART_PORT].port); */
- /* EXPORT_SYMBOL(p_mtk_uart_port); */
- struct mtk_uart *mt_console_uart = &(mtk_uarts[DEFAULT_FIQ_UART_PORT]);
- #endif
- /* ============================================================================== */
- /* --------------------------------------------------------------------------- */
- /* UART Log port switch feature */
- static int find_string(char str[], const char *fingerprint, int *offset)
- {
- char *curr = str;
- int i = 0;
- int str_len;
- int fingerprint_len;
- if ((NULL == str) || (NULL == fingerprint))
- return 0;
- str_len = strlen(str);
- fingerprint_len = strlen(fingerprint);
- if (str_len < fingerprint_len)
- return 0;
- for (i = 0; i <= (str_len - fingerprint_len); i++) {
- if (strncmp(curr, fingerprint, fingerprint_len) == 0) {
- if (NULL != offset)
- *offset = i;
- return 1;
- }
- curr++;
- }
- return 0;
- }
- static int find_fingerprint(char str[], int *offset)
- {
- /**
- * This function limitation:
- * If the ttyMT number large than 9, this function will work abnormal.
- * For example, ttyMT12 will be recoginzed as ttyMT1
- */
- static const char * const fingerprint[] = {
- "console=/dev/null",
- "console=ttyMT0",
- "console=ttyMT1",
- "console=ttyMT2",
- "console=ttyMT3",
- #if (UART_NR > 4)
- "console=ttyMT4",
- #endif
- };
- int i;
- for (i = 0; i < sizeof(fingerprint) / sizeof(char *); i++) {
- if (find_string(str, fingerprint[i], offset) != 0)
- return i; /* Find it. */
- }
- return -1; /* Not find */
- }
- static int modify_fingerprint(char str[], int offset, char new_val)
- {
- if (NULL == str)
- return 0;
- str[offset + 14 - 1] = new_val; /* 14 = strlen("console=ttyMTx"), we modify x to 1~3 */
- return 1;
- }
- void adjust_kernel_cmd_line_setting_for_console(char *u_boot_cmd_line, char *kernel_cmd_line)
- {
- int offset = 0;
- int kernel_console_port_setting = -1;
- int u_boot_console_port_setting = -1;
- /* Check u-boot command line setting */
- u_boot_console_port_setting = find_fingerprint(u_boot_cmd_line, 0);
- if (-1 == u_boot_console_port_setting) {
- /* printf("U-boot does not have console setting, return\n"); */
- return;
- }
- /* U-boot has console setting, check kernel console setting */
- kernel_console_port_setting = find_fingerprint(kernel_cmd_line, &offset);
- if (-1 == kernel_console_port_setting) {
- /* printf("Kernel does not have console setting, return\n"); */
- goto _Exit;
- }
- /**
- * Both U-boot and Kernel has console setting.
- * 1. If the settings are same, return directly;
- * 2. If kernel console setting is null, then use kernel setting
- * 3. If u-boot console setting is null, then use kernel setting
- * 4. If kernel console setting is not null, then use u-boot setting
- */
- if (u_boot_console_port_setting == kernel_console_port_setting) {
- /* printf("Same console setting, return\n"); */
- goto _Exit;
- }
- if (0 == kernel_console_port_setting) {
- /* printf("Kernel console setting is null, use kernel setting, return\n"); */
- goto _Exit;
- }
- if (0 == u_boot_console_port_setting) {
- /* printf("U-boot console setting is null, use kernel setting, return\n"); */
- goto _Exit;
- }
- /* Enter here, it means both kernel and u-boot console setting are not null, using u-boot setting */
- switch (u_boot_console_port_setting) {
- case 1: /* Using ttyMT0 */
- modify_fingerprint(kernel_cmd_line, offset, '0');
- break;
- case 2: /* Using ttyMT1 */
- modify_fingerprint(kernel_cmd_line, offset, '1');
- break;
- case 3: /* Using ttyMT2 */
- modify_fingerprint(kernel_cmd_line, offset, '2');
- break;
- case 4: /* Using ttyMT3 */
- modify_fingerprint(kernel_cmd_line, offset, '3');
- break;
- case 5: /* Using ttyMT4 */
- modify_fingerprint(kernel_cmd_line, offset, '4');
- break;
- default:
- /* Do nothing */
- break;
- }
- _Exit:
- kernel_console_port_setting = 0;
- #if (defined(CONFIG_FIQ_DEBUGGER_CONSOLE) && defined(CONFIG_FIQ_DEBUGGER))
- kernel_console_port_setting = find_fingerprint(kernel_cmd_line, &offset);
- if (-1 == kernel_console_port_setting) {
- /* printf("Kernel does not have console setting, return\n"); */
- return;
- }
- if (kernel_console_port_setting > 0) {
- fiq_console_port = (kernel_console_port_setting - 1);
- mt_console_uart = &(mtk_uarts[fiq_console_port]);
- }
- #endif
- }
- /* ============================================================================== */
- /* -------------------------------- PDN --------------------------------------- */
- unsigned int mtk_uart_pdn_enable(char *port, int enable)
- {
- int str_len;
- int port_num;
- if (port == NULL)
- return -1;
- str_len = strlen(port);
- if (str_len != 6) {
- MSG_ERR("Length mismatch! len=%d\n", str_len);
- return -1;
- }
- if (find_string(port, "ttyMT", 0) == 0) {
- MSG_ERR("Format mismatch! str=%s\n", port);
- return -1;
- }
- port_num = port[str_len - 1] - '0';
- if (port_num >= UART_NR) {
- MSG_ERR("wrong port:%d\n", port_num);
- return -1;
- }
- bt_port = &mtk_uarts[port_num];
- if (enable)
- mtk_uart_enable_dpidle(bt_port);
- else
- mtk_uart_disable_dpidle(bt_port);
- return 0;
- }
- unsigned int mtk_uart_freeze_enable(char *port, int enable)
- {
- int str_len;
- int port_num;
- if (port == NULL)
- return -1;
- str_len = strlen(port);
- if (str_len != 6) {
- MSG_ERR("Length mismatch! len=%d\n", str_len);
- return -1;
- }
- if (find_string(port, "ttyMT", 0) == 0) {
- MSG_ERR("Format mismatch! str=%s\n", port);
- return -1;
- }
- port_num = port[str_len - 1] - '0';
- if (port_num >= UART_NR) {
- MSG_ERR("wrong port:%d\n", port_num);
- return -1;
- }
- if (enable)
- uart_freeze_enable[port_num] = 1;
- else
- uart_freeze_enable[port_num] = 0;
- return 0;
- }
- EXPORT_SYMBOL(mtk_uart_freeze_enable);
- /*---------------------------------------------------------------------------*/
- #ifdef CONFIG_MTK_SERIAL_CONSOLE
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_console_write(struct console *co, const char *s, unsigned int count)
- {
- /* Notice:
- * (1) The function is called by printk, hence, spin lock can not be used
- * (2) don't care vfifo setting
- */
- #define CONSOLE_RETRY (5000)
- int i;
- struct mtk_uart *uart;
- u32 cnt = 0;
- unsigned long flags;
- if (co->index >= UART_NR || !(co->flags & CON_ENABLED) || !atomic_read(&mtk_uart_sysobj.console_enable))
- return;
- uart = &mtk_uarts[co->index];
- for (i = 0; i < (int)count; i++) {
- cnt = 0;
- while (!mtk_uart_write_allow(uart)) {
- barrier();
- if (cnt++ >= CONSOLE_RETRY) {
- uart->timeout_count++;
- return;
- }
- }
- spin_lock_irqsave(&mtk_console_lock, flags);
- mtk_uart_write_byte(uart, s[i]);
- spin_unlock_irqrestore(&mtk_console_lock, flags);
- if (s[i] == '\n') {
- cnt = 0;
- while (!mtk_uart_write_allow(uart)) {
- barrier();
- if (cnt++ >= CONSOLE_RETRY) {
- uart->timeout_count++;
- return;
- }
- }
- spin_lock_irqsave(&mtk_console_lock, flags);
- mtk_uart_write_byte(uart, '\r');
- spin_unlock_irqrestore(&mtk_console_lock, flags);
- }
- }
- }
- /*---------------------------------------------------------------------------*/
- static int __init mtk_uart_console_setup(struct console *co, char *options)
- {
- struct mtk_uart *uart;
- struct uart_port *port;
- int baud = 115200;
- int bits = 8;
- int parity = 'n';
- int flow = 'n';
- int ret;
- pr_debug("[UART]mtk console setup : co->index %d options:%s\n", co->index, options);
- if (co->index >= UART_NR)
- co->index = 0;
- uart = &mtk_uarts[co->index];
- port = (struct uart_port *)uart;
- console_port = uart;
- mtk_uart_console_setting_switch(uart);
- if (options)
- uart_parse_options(options, &baud, &parity, &bits, &flow);
- ret = uart_set_options(port, co, baud, parity, bits, flow);
- pr_debug("[UART]mtk console setup: uart_set_option port(%d) baud(%d) parity(%c) bits(%d) flow(%c) - ret(%d)\n"
- , co->index, baud, parity, bits, flow, ret);
- pr_debug("[UART]mtk setting: (%d, %d, %d, %lu, %lu)\n",
- uart->tx_mode, uart->rx_mode, uart->dma_mode, uart->tx_trig, uart->rx_trig);
- /* mtk_uart_power_up(uart); */
- return ret;
- }
- /*---------------------------------------------------------------------------*/
- static struct uart_driver mtk_uart_drv;
- static struct console mtk_uart_console = {
- .name = "ttyMT",
- #if !defined(CONFIG_MTK_SERIAL_MODEM_TEST)
- /*don't configure UART4 as console */
- .write = mtk_uart_console_write,
- .setup = mtk_uart_console_setup,
- #endif
- .device = uart_console_device,
- .flags = CON_PRINTBUFFER,
- .index = -1,
- .data = &mtk_uart_drv,
- };
- /*---------------------------------------------------------------------------*/
- static int __init mtk_uart_console_init(void)
- {
- int err = mtk_uart_init_ports();
- if (!err)
- register_console(&mtk_uart_console);
- return err;
- }
- /*---------------------------------------------------------------------------*/
- console_initcall(mtk_uart_console_init);
- /*---------------------------------------------------------------------------*/
- static int __init mtk_late_console_init(void)
- {
- if (!(mtk_uart_console.flags & CON_ENABLED))
- register_console(&mtk_uart_console);
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- late_initcall(mtk_late_console_init);
- /*---------------------------------------------------------------------------*/
- #endif /*CONFIG_MTK_SERIAL_CONSOLE */
- /******************************************************************************
- * Virtual FIFO implementation
- ******************************************************************************/
- #if defined(ENABLE_VFIFO)
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_vfifo_del_dbgbuf(struct mtk_uart_vfifo *vfifo)
- {
- #if defined(ENABLE_VFIFO_DEBUG)
- int idx;
- for (idx = 0; idx < ARRAY_SIZE(vfifo->dbg); idx++) {
- if (vfifo->dbg[idx].dat != 0)
- kfree(vfifo->dbg[idx].dat);
- vfifo->dbg[idx].dat = NULL;
- vfifo->dbg[idx].idx = 0;
- vfifo->dbg[idx].len = 0;
- }
- #endif
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_vfifo_new_dbgbuf(struct mtk_uart_vfifo *vfifo)
- {
- #if defined(ENABLE_VFIFO_DEBUG)
- int idx;
- for (idx = 0; idx < ARRAY_SIZE(vfifo->dbg); idx++) {
- if (vfifo->dbg[idx].dat != 0)
- kfree(vfifo->dbg[idx].dat);
- vfifo->dbg[idx].idx = 0;
- vfifo->dbg[idx].len = vfifo->size;
- vfifo->dbg[idx].dat = kzalloc(vfifo->dbg[idx].len, GFP_ATOMIC);
- if (!vfifo->dbg[idx].dat) {
- mtk_uart_vfifo_del_dbgbuf(vfifo);
- return -ENOMEM;
- }
- }
- #endif
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_vfifo_create(struct mtk_uart *uart)
- { /*NOTE: please save the phyiscal address in vff->dmahd */
- struct mtk_uart_vfifo *vfifo;
- int idx, err = 0;
- MSG_FUNC_ENTRY();
- if (!uart->setting->vff) {
- MSG_RAW("[UART%2d] not support VFF, Cancel alloc\n", uart->nport);
- return err;
- }
- for (idx = uart->nport * 2; idx < uart->nport * 2 + 2; idx++) {
- vfifo = &mtk_uart_vfifo_port[idx];
- if (vfifo->size) {
- vfifo->addr = dma_alloc_coherent(uart->port.dev, vfifo->size, &vfifo->dmahd, GFP_DMA);
- /* MSG_RAW("Address: virt = 0x%p, phys = 0x%llx\n", vfifo->addr, vfifo->dmahd); */
- } else {
- vfifo->addr = NULL;
- }
- if (vfifo->size && !vfifo->addr) {
- err = -ENOMEM;
- break;
- }
- err = mtk_uart_vfifo_new_dbgbuf(vfifo);
- if (err)
- break;
- }
- return err;
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_vfifo_delete(struct mtk_uart *uart)
- {
- struct mtk_uart_vfifo *vfifo;
- int idx;
- MSG_FUNC_ENTRY();
- if (!uart->setting->vff) {
- MSG_RAW("[UART%2d] not support VFF, Cancel free\n", uart->nport);
- return 0;
- }
- MSG_RAW("[UART%2d] delete", uart->nport);
- for (idx = uart->nport * 2; idx < uart->nport * 2 + 2; idx++) {
- vfifo = &mtk_uart_vfifo_port[idx];
- if (vfifo->addr)
- dma_free_coherent(NULL, vfifo->size, vfifo->addr, vfifo->dmahd);
- mtk_uart_vfifo_del_dbgbuf(vfifo);
- MSG_RAW("[%2d] %p (%04d) ;", idx, vfifo->addr, vfifo->size);
- vfifo->addr = NULL;
- }
- MSG_RAW("\n");
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- int mtk_uart_vfifo_prepare(struct mtk_uart *uart)
- {
- struct mtuart_sysobj *obj = &mtk_uart_sysobj;
- struct mtk_uart_vfifo *tport, *rport;
- int tx = uart->nport << 1;
- int rx = tx + 1;
- MSG_FUNC_ENTRY();
- if (uart->nport >= UART_NR) {
- MSG_ERR("wrong port:%d\n", uart->nport);
- return -EINVAL;
- } else if (FALSE == uart->setting->vff) {
- MSG_ERR("Port :%d not support vfifo\n", uart->nport);
- return -EINVAL;
- }
- tport = &mtk_uart_vfifo_port[tx];
- rport = &mtk_uart_vfifo_port[rx];
- if ((atomic_read(&obj->vffLen[tx]) == tport->size) && (atomic_read(&obj->vffLen[rx]) == rport->size))
- return 0;
- MSG_RAW("re-alloc +\n");
- mtk_uart_vfifo_delete(uart);
- tport->size = atomic_read(&obj->vffLen[tx]);
- tport->trig = VFF_TX_THRE(tport->size);
- rport->size = atomic_read(&obj->vffLen[rx]);
- rport->trig = VFF_RX_THRE(rport->size);
- mtk_uart_vfifo_create(uart);
- MSG_RAW("re-alloc -\n");
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- static struct mtk_uart_vfifo *mtk_uart_vfifo_alloc(struct mtk_uart *uart, UART_VFF_TYPE type)
- {
- struct mtk_uart_vfifo *vfifo = NULL;
- unsigned long flags;
- spin_lock_irqsave(&mtk_uart_vfifo_port_lock, flags);
- MSG(INFO, "(%d, %d)", uart->nport, type);
- if ((uart->nport >= (ARRAY_SIZE(mtk_uart_vfifo_port) / 2)) || (type >= UART_VFIFO_NUM))
- vfifo = NULL;
- else
- vfifo = &mtk_uart_vfifo_port[2 * uart->nport + type];
- if (vfifo && vfifo->addr == NULL)
- vfifo = NULL;
- if (vfifo)
- MSG(INFO, "alloc vfifo-%d[%d](%p)\n", uart->nport, vfifo->size, vfifo->addr);
- spin_unlock_irqrestore(&mtk_uart_vfifo_port_lock, flags);
- return vfifo;
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_vfifo_free(struct mtk_uart *uart, struct mtk_uart_vfifo *vfifo)
- {
- unsigned long flags;
- if (vfifo) {
- spin_lock_irqsave(&mtk_uart_vfifo_port_lock, flags);
- vfifo->dma = NULL;
- vfifo->cur = NULL;
- vfifo->dbgidx = 0;
- spin_unlock_irqrestore(&mtk_uart_vfifo_port_lock, flags);
- }
- }
- /*---------------------------------------------------------------------------*/
- static unsigned int mtk_uart_vfifo_write_allow(struct mtk_uart *uart)
- {
- return !mtk_uart_vfifo_is_full(uart->tx_vfifo);
- }
- /*---------------------------------------------------------------------------*/
- static unsigned int mtk_uart_vfifo_read_allow(struct mtk_uart *uart)
- {
- return !mtk_uart_vfifo_is_empty(uart->rx_vfifo);
- }
- /*---------------------------------------------------------------------------*/
- static inline unsigned short mtk_uart_vfifo_get_trig(struct mtk_uart *uart, struct mtk_uart_vfifo *vfifo)
- {
- return vfifo->trig;
- }
- /*---------------------------------------------------------------------------*/
- #define get_mtk_uart(ptr, type, member) (type *)((char *)ptr - offsetof(type, member))
- /*---------------------------------------------------------------------------*/
- static enum hrtimer_restart mtk_uart_tx_vfifo_timeout(struct hrtimer *hrt)
- {
- struct mtk_uart_vfifo *vfifo = container_of(hrt, struct mtk_uart_vfifo, flush);
- struct mtk_uart_dma *dma = (struct mtk_uart_dma *)vfifo->dma;
- struct mtk_uart *uart = dma->uart;
- #if defined(ENABLE_VFIFO_DEBUG)
- ktime_t cur = ktime_get();
- struct timespec a = ktime_to_timespec(cur);
- MSG(MSC, "flush timeout [%ld %ld]\n", a.tv_sec, a.tv_nsec);
- #endif
- mtk_uart_tx_vfifo_flush(uart, 1);
- return HRTIMER_NORESTART;
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_dma_vfifo_callback(void *data)
- {
- struct mtk_uart_dma *dma = (struct mtk_uart_dma *)data;
- struct mtk_uart *uart = dma->uart;
- MSG(DMA, "%s VFIFO CB: %4d/%4d\n", dma->dir == DMA_TO_DEVICE ? "TX" : "RX",
- mtk_uart_vfifo_get_counts(dma->vfifo), dma->vfifo->size);
- if (dma->dir == DMA_FROM_DEVICE) {
- /*the data must be read before return from callback, otherwise, the interrupt
- will be triggered again and again */
- mtk_uart_dma_vfifo_rx_tasklet((unsigned long)uart);
- /* return; [ALPS00031975] */
- }
- tasklet_schedule(&dma->tasklet);
- }
- /*---------------------------------------------------------------------------*/
- /* static __tcmfunc irqreturn_t mtk_vfifo_irq_handler(int irq, void *dev_id) */
- static irqreturn_t mtk_vfifo_irq_handler(int irq, void *dev_id)
- {
- struct mtk_uart_vfifo *vfifo;
- vfifo = (struct mtk_uart_vfifo *)dev_id;
- if (!vfifo) {
- pr_err("mtk_vfifo_irq_handler: vfifo is NULL\n");
- return IRQ_NONE;
- }
- if (!vfifo->dma) {
- pr_err("mtk_vfifo_irq_handler: dma is NULL\n");
- return IRQ_NONE;
- }
- /* Call call back function */
- mtk_uart_dma_vfifo_callback(vfifo->dma);
- /* Clear interrupt flag */
- if (vfifo->type == UART_RX_VFIFO)
- mtk_uart_vfifo_clear_rx_intr(vfifo);
- else
- mtk_uart_vfifo_clear_tx_intr(vfifo);
- return IRQ_HANDLED;
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_dma_alloc(struct mtk_uart *uart, struct mtk_uart_dma *dma, int mode, struct mtk_uart_vfifo *vfifo)
- {
- int ret = 0;
- MSG_FUNC_ENTRY();
- if (mode == UART_NON_DMA)
- return -1;
- switch (mode) {
- case UART_TX_VFIFO_DMA:
- if (!vfifo) {
- MSG(ERR, "fail due to NULL tx_vfifo\n");
- ret = -1;
- break;
- }
- vfifo->dma = dma;
- dma->dir = DMA_TO_DEVICE;
- dma->mode = mode;
- dma->vfifo = vfifo;
- dma->uart = uart;
- init_completion(&dma->done);
- tasklet_init(&dma->tasklet, mtk_uart_dma_vfifo_tx_tasklet, (unsigned long)uart);
- if (!atomic_read(&vfifo->reg_cb)) {
- /* disable interrupts */
- /* FIXME */
- mtk_uart_vfifo_disable_tx_intr(uart);
- ret = request_irq(vfifo->irq_id, (irq_handler_t) mtk_vfifo_irq_handler,
- IRQF_LEVEL_TRIGGER_POLARITY, DRV_NAME, vfifo);
- if (ret)
- return ret;
- atomic_set(&vfifo->reg_cb, 1);
- }
- atomic_set(&dma->free, 1);
- break;
- case UART_RX_VFIFO_DMA:
- if (!vfifo) {
- MSG(ERR, "fail due to NULL rx_vfifo\n");
- ret = -1;
- break;
- }
- vfifo->dma = dma;
- dma->dir = DMA_FROM_DEVICE;
- dma->mode = mode;
- dma->vfifo = vfifo;
- dma->uart = uart;
- init_completion(&dma->done);
- tasklet_init(&dma->tasklet, mtk_uart_dma_vfifo_rx_tasklet, (unsigned long)uart);
- if (!atomic_read(&vfifo->reg_cb)) {
- /* disable interrupts */
- mtk_uart_vfifo_disable_rx_intr(uart);
- ret = request_irq(vfifo->irq_id, (irq_handler_t) mtk_vfifo_irq_handler,
- IRQF_LEVEL_TRIGGER_POLARITY, DRV_NAME, vfifo);
- if (ret)
- return ret;
- atomic_set(&vfifo->reg_cb, 1);
- }
- atomic_set(&dma->free, 1);
- break;
- }
- return ret;
- }
- /*---------------------------------------------------------------------------*/
- void mtk_uart_dma_stop(struct mtk_uart *uart, struct mtk_uart_dma *dma)
- {
- MSG_FUNC_ENTRY();
- if (!dma)
- return;
- mtk_uart_stop_dma(dma);
- atomic_set(&dma->free, 1);
- complete(&dma->done);
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_dma_free(struct mtk_uart *uart, struct mtk_uart_dma *dma)
- {
- unsigned long flags;
- MSG_FUNC_ENTRY();
- if (!dma)
- return;
- if (dma->mode == UART_NON_DMA)
- return;
- if ((dma->mode == UART_RX_VFIFO_DMA || dma->mode == UART_TX_VFIFO_DMA) && (!dma->vfifo))
- return;
- if (dma->vfifo && !mtk_uart_vfifo_is_empty(dma->vfifo)) {
- tasklet_schedule(&dma->tasklet);
- MSG(DMA, "wait for %s vfifo dma completed!!!\n", dma->dir == DMA_TO_DEVICE ? "TX" : "RX");
- wait_for_completion(&dma->done);
- }
- spin_lock_irqsave(&uart->port.lock, flags);
- mtk_uart_stop_dma(dma);
- if (dma->vfifo && timer_pending(&dma->vfifo->timer))
- del_timer_sync(&dma->vfifo->timer);
- if (dma->vfifo && hrtimer_active(&dma->vfifo->flush))
- hrtimer_cancel(&dma->vfifo->flush);
- /* [ALPS00030487] tasklet_kill function may schedule, so release spin lock first,
- * after release, set spin lock again.
- */
- spin_unlock_irqrestore(&uart->port.lock, flags); /* [ALPS00030487] Add this */
- tasklet_kill(&dma->tasklet);
- spin_lock_irqsave(&uart->port.lock, flags); /* [ALPS00030487] Add this */
- mtk_uart_reset_dma(dma);
- mtk_uart_vfifo_disable(uart, dma->vfifo);
- mtk_uart_vfifo_free(uart, dma->vfifo);
- MSG(INFO, "free %s dma completed!!!\n", dma->dir == DMA_TO_DEVICE ? "TX" : "RX");
- memset(dma, 0, sizeof(struct mtk_uart_dma));
- spin_unlock_irqrestore(&uart->port.lock, flags);
- }
- #endif /*defined(ENABLE_VFIFO) */
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_set_baud(struct mtk_uart *uart, int baudrate)
- {
- if (uart->port.flags & ASYNC_SPD_CUST) {
- /**
- * [ALPS00137126] Begin
- * Because the origin design of custom baudrate in linux is for low speed case, we add some
- * modify to support high speed case.
- * NOTE: If the highest bit of "custom_divisor" is ONE, we will use custom_divisor store baudrate
- * directly. That means(we suppose unsigned int is 32 bits):
- * custom_divisor[31] == 1, then custom_divisor[30..0] == custom baud rate
- * custom_divisor[31] == 0, then custom_divisor[30..0] == sysclk/16/baudrate
- */
- if (uart->port.custom_divisor & (1 << 31)) {
- baudrate = uart->port.custom_divisor & (~(1 << 31));
- if (baudrate > (uart->sysclk >> 2)) /* Baud rate should not more than sysclk/4 */
- baudrate = 9600;
- } else {
- /*the baud_base gotten in user space eqauls to sysclk/16.
- hence, we need to restore the difference when calculating custom baudrate */
- if (!uart->custom_baud) {
- baudrate = uart->sysclk / 16;
- baudrate = baudrate / uart->port.custom_divisor;
- } else {
- baudrate = uart->custom_baud;
- }
- /* [ALPS00137126] End */
- }
- MSG(CFG, "CUSTOM, baudrate = %d, divisor = %d\n", baudrate, uart->port.custom_divisor);
- }
- if (uart->auto_baud)
- mtk_uart_set_auto_baud(uart);
- mtk_uart_baud_setting(uart, baudrate);
- uart->baudrate = baudrate;
- }
- /*---------------------------------------------------------------------------*/
- static inline bool mtk_uart_enable_sysrq(struct mtk_uart *uart)
- {
- return uart->setting->sysrq;
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_rx_chars(struct mtk_uart *uart)
- {
- struct uart_port *port = &uart->port;
- struct tty_struct *tty = uart->port.state->port.tty;
- int max_count = UART_FIFO_SIZE;
- unsigned int data_byte, status;
- unsigned int flag;
- unsigned long flags;
- spin_lock_irqsave(&port->lock, flags);
- /* MSG_FUNC_ENTRY(); */
- while (max_count-- > 0) {
- /* check status */
- if (!mtk_uart_data_ready(uart))
- break;
- #if 0
- if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
- if (tty->low_latency) {
- /*
- * If this failed then we will throw away the
- * bytes but must do so to clear interrupts
- */
- tty_flip_buffer_push(tty);
- }
- }
- #endif
- /* read the byte */
- data_byte = uart->read_byte(uart);
- port->icount.rx++;
- flag = TTY_NORMAL;
- update_history_byte(0, uart->nport, data_byte);
- status = mtk_uart_filter_line_status(uart);
- /* error handling routine */
- if (status & UART_LSR_BI) {
- MSG(INFO, "Break interrupt!!\n");
- port->icount.brk++;
- if (uart_handle_break(port))
- continue;
- flag = TTY_BREAK;
- } else if (status & UART_LSR_PE) {
- MSG(INFO, "Parity Error!!\n");
- port->icount.parity++;
- flag = TTY_PARITY;
- } else if (status & UART_LSR_FE) {
- MSG(INFO, "Frame Error!!\n");
- port->icount.frame++;
- flag = TTY_FRAME;
- } else if (status & UART_LSR_OE) {
- MSG(INFO, "Overrun!!\n");
- port->icount.overrun++;
- flag = TTY_OVERRUN;
- }
- #ifdef CONFIG_MAGIC_SYSRQ
- if (mtk_uart_enable_sysrq(uart)) {
- if (uart_handle_sysrq_char(port, data_byte))
- continue;
- /* FIXME. Infinity, 20081002, 'BREAK' char to enable sysrq handler { */
- #if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSLE)
- if (data_byte == 0)
- uart->port.sysrq = 1;
- #endif
- /* FIXME. Infinity, 20081002, 'BREAK' char to enable sysrq handler } */
- }
- #endif
- if (!tty_insert_flip_char(TTY_FLIP_ARG(tty), data_byte, flag))
- MSG(ERR, "tty_insert_flip_char: no space");
- }
- tty_flip_buffer_push(TTY_FLIP_ARG(tty));
- update_history_time(0, uart->nport);
- spin_unlock_irqrestore(&port->lock, flags);
- MSG(FUC, "%s (%2d)\n", __func__, UART_FIFO_SIZE - max_count - 1);
- /*
- #if defined(CONFIG_MTK_HDMI_SUPPORT)
- #ifdef MHL_UART_SHARE_PIN
- if ((UART_FIFO_SIZE - max_count - 1) > 0)
- hdmi_force_on(UART_FIFO_SIZE - max_count - 1);
- #endif
- #endif
- */
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_tx_chars(struct mtk_uart *uart)
- {
- /* Notice:
- * The function is called by uart_start, which is protected by spin lock,
- * Hence, no spin-lock is required in the functions
- */
- struct uart_port *port = &uart->port;
- struct circ_buf *xmit = &port->state->xmit;
- int count;
- /* deal with x_char first */
- if (unlikely(port->x_char)) {
- MSG(INFO, "detect x_char!!\n");
- uart->write_byte(uart, port->x_char);
- port->icount.tx++;
- port->x_char = 0;
- return;
- }
- /* stop tx if circular buffer is empty or this port is stopped */
- if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
- struct tty_struct *tty = port->state->port.tty;
- if (!uart_circ_empty(xmit))
- MSG(ERR, "\t\tstopped: empty: %d %d %d\n", uart_circ_empty(xmit), tty->stopped,
- tty->hw_stopped);
- mtk_uart_stop_tx(port);
- return;
- }
- count = port->fifosize - 1;
- do {
- if (uart_circ_empty(xmit))
- break;
- if ((count == 1) || (!uart->write_allow(uart))) {
- /* to avoid the interrupt is not enable. */
- mtk_uart_enable_intrs(uart, UART_IER_ETBEI);
- break;
- }
- uart->write_byte(uart, xmit->buf[xmit->tail]);
- update_history_byte(1, uart->nport, xmit->buf[xmit->tail]);
- xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
- port->icount.tx++;
- } while (--count > 0);
- MSG(INFO, "TX %d chars\n", port->fifosize - 1 - count);
- if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
- uart_write_wakeup(port);
- if (uart_circ_empty(xmit))
- mtk_uart_stop_tx(port);
- update_history_time(1, uart->nport);
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_rx_handler(struct mtk_uart *uart, int intrs)
- {
- if (uart->rx_mode == UART_NON_DMA) {
- mtk_uart_rx_chars(uart);
- } else if (uart->rx_mode == UART_RX_VFIFO_DMA) {
- #if defined(ENABLE_VFIFO)
- mtk_uart_rx_pre_handler(uart, intrs);
- mtk_uart_dma_vfifo_rx_tasklet((unsigned long)uart);
- #endif
- }
- }
- /*---------------------------------------------------------------------------*/
- void mtk_uart_tx_handler(struct mtk_uart *uart)
- {
- struct uart_port *port = &uart->port;
- unsigned long flags;
- if (uart->tx_mode == UART_NON_DMA) {
- spin_lock_irqsave(&port->lock, flags);
- mtk_uart_tx_chars(uart);
- spin_unlock_irqrestore(&port->lock, flags);
- } else if (uart->tx_mode == UART_TX_VFIFO_DMA) {
- tasklet_schedule(&uart->dma_tx.tasklet);
- }
- }
- /*---------------------------------------------------------------------------*/
- #ifdef ENABLE_DEBUG
- /*---------------------------------------------------------------------------*/
- static const char * const fifo[] = { "No FIFO", "Unstable FIFO",
- "Unknown", "FIFO Enabled"
- };
- static const char * const interrupt[] = { "Modem Status Chg", "Tx Buffer Empty",
- "Rx Data Received", "BI, FE, PE, or OE",
- "0x04", "0x05", "Rx Data Timeout", "0x07",
- "SW Flow Control", "0x09", "0x10", "0x11", "0x12",
- "0x13", "0x14", "0x15", "HW Flow Control"
- };
- /*---------------------------------------------------------------------------*/
- #endif
- /*---------------------------------------------------------------------------*/
- /* static __tcmfunc irqreturn_t mtk_uart_irq(int irq, void *dev_id) */
- static irqreturn_t mtk_uart_irq(int irq, void *dev_id)
- {
- unsigned int intrs, timeout = 0;
- struct mtk_uart *uart = (struct mtk_uart *)dev_id;
- #ifndef CONFIG_FIQ_DEBUGGER
- #ifdef CONFIG_MT_PRINTK_UART_CONSOLE
- unsigned long base;
- base = uart->base;
- if ((uart == console_port) && (UART_READ32(UART_LSR) & 0x01))
- printk_disable_uart = 0;
- #endif
- #endif
- intrs = mtk_uart_get_interrupt(uart);
- #ifdef ENABLE_DEBUG
- {
- UART_IIR_REG *iir = (UART_IIR_REG *) &intrs;
- if (iir->NINT)
- MSG(INT, "No interrupt (%s)\n", fifo[iir->FIFOE]);
- else if (iir->ID < ARRAY_SIZE(interrupt))
- MSG(INT, "%02x %s (%s)\n", iir->ID, interrupt[iir->ID], fifo[iir->FIFOE]);
- else
- MSG(INT, "%2x\n", iir->ID);
- }
- #endif
- intrs &= UART_IIR_INT_MASK;
- if (intrs == UART_IIR_NO_INT_PENDING)
- return IRQ_HANDLED;
- /* pr_debug("[UART%d] intrs:0x%x\n", uart->nport, intrs); */
- if (intrs == UART_IIR_CTI)
- timeout = 1;
- else if (intrs == UART_IIR_THRE)
- mtk_uart_tx_handler(uart);
- else if (intrs == UART_IIR_MS)
- mtk_uart_get_modem_status(uart);
- mtk_uart_intr_last_check(uart, intrs);
- mtk_uart_rx_handler(uart, intrs);
- return IRQ_HANDLED;
- }
- /*---------------------------------------------------------------------------*/
- /* test whether the transmitter fifo and shifter for the port is empty. */
- static unsigned int mtk_uart_tx_empty(struct uart_port *port)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- MSG_FUNC_ENTRY();
- #if defined(ENABLE_VFIFO)
- if (uart->tx_mode == UART_TX_VFIFO_DMA)
- return mtk_uart_vfifo_is_empty(uart->dma_tx.vfifo) ? TIOCSER_TEMT : 0;
- #endif
- return uart->write_allow(uart) ? TIOCSER_TEMT : 0;
- }
- /*---------------------------------------------------------------------------*/
- /* FIXME */
- /* stop transmitting characters
- * Note: this function is call with interrupt disabled
- */
- static void mtk_uart_stop_tx(struct uart_port *port)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- MSG_FUNC_ENTRY();
- #if defined(ENABLE_VFIFO)
- if (uart->tx_mode == UART_TX_VFIFO_DMA) {
- /*1. UART_IER_ETBEI can't be disabled or zero data appears in TX */
- /*2. TX_INT_EN.INTEN will be reset automatically by HW */
- } else
- #endif
- /* disable tx interrupt */
- mtk_uart_disable_intrs(uart, UART_IER_ETBEI);
- uart->tx_stop = 1;
- }
- /*---------------------------------------------------------------------------*/
- /* FIXME */
- /* start transmitting characters.
- * Note: this function is call with interrupt disabled
- */
- static void mtk_uart_start_tx(struct uart_port *port)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- struct circ_buf *xmit = &port->state->xmit;
- unsigned long size;
- size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
- if (!size)
- return;
- uart->tx_stop = 0;
- #ifdef ENABLE_RAW_DATA_DUMP
- reset_tx_raw_data(uart);
- #endif
- #if defined(ENABLE_VFIFO)
- if (uart->tx_mode == UART_TX_VFIFO_DMA) {
- if (UART_DEBUG_EVT(DBG_EVT_BUF))
- pr_debug("[UART%d] mtk_uart_start_tx\n", uart->nport);
- if (!uart->write_allow(uart))
- mtk_uart_vfifo_enable_tx_intr(uart);
- else
- mtk_uart_dma_vfifo_tx_tasklet((unsigned long)uart);
- } else {
- #else
- {
- #endif
- if (uart->write_allow(uart))
- mtk_uart_tx_chars(uart);
- }
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_send_xchar(struct uart_port *port, char ch)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- unsigned long flags;
- MSG_FUNC_ENTRY();
- if (uart->tx_stop)
- return;
- spin_lock_irqsave(&port->lock, flags);
- while (!mtk_uart_write_allow(uart))
- ;
- mtk_uart_write_byte(uart, (unsigned char)ch);
- port->icount.tx++;
- spin_unlock_irqrestore(&port->lock, flags);
- }
- /*---------------------------------------------------------------------------*/
- /* enable the modem status interrupts */
- static void mtk_uart_enable_ms(struct uart_port *port)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- MSG_FUNC_ENTRY();
- uart->ms_enable = 1;
- }
- /*---------------------------------------------------------------------------*/
- /* grab any interrupt resources and initialize any low level driver state */
- static int mtk_uart_startup(struct uart_port *port)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- int ret;
- long mask = UART_IER_HW_NORMALINTS;
- MSG_FUNC_ENTRY();
- /*the uart port is power up in power_mgnt */
- /* Reset default flag when the uart starts up, or the previous setting,
- * Such as custom baudrate will be still applied even it is ever closed
- */
- /* uart->port.flags = UPF_BOOT_AUTOCONF; */
- /* uart->port.custom_divisor = 1; */
- /* Check whether is ATE_Factory mode */
- #ifdef ATE_FACTORY_ENABLE
- mtk_uart_is_ate_factory_mode(uart);
- #endif /*ATE_FACTORY_ENABLE */
- uart->fctl_mode = UART_FC_NONE;
- /* disable interrupts */
- mtk_uart_disable_intrs(uart, UART_IER_ALL_INTS);
- /* allocate irq line */
- /* ret = request_irq(port->irq, mtk_uart_irq, 0, DRV_NAME, uart); */
- #ifdef CONFIG_OF
- /* [ALPS00142658] Fix incompatible pointer type waning */
- ret = request_irq(port->irq, (irq_handler_t) mtk_uart_irq, uart->setting->irq_flags, DRV_NAME, uart);
- #else
- /* [ALPS00142658] Fix incompatible pointer type waning */
- ret = request_irq(port->irq, (irq_handler_t) mtk_uart_irq, IRQF_LEVEL_TRIGGER_POLARITY, DRV_NAME, uart);
- #endif
- if (ret)
- return ret;
- #if defined(ENABLE_VFIFO)
- #if defined(ENABLE_VFIFO_DEBUG)
- mtk_uart_vfifo_prepare(uart);
- uart->dma_mode = uart->setting->dma_mode;
- uart->tx_mode = uart->setting->tx_mode;
- uart->rx_mode = uart->setting->rx_mode;
- uart->tx_trig = uart->setting->tx_trig;
- uart->rx_trig = uart->setting->rx_trig;
- #endif
- /* allocate vfifo */
- if (uart->rx_mode == UART_RX_VFIFO_DMA) {
- uart->rx_vfifo = mtk_uart_vfifo_alloc(uart, UART_RX_VFIFO);
- ret = mtk_uart_dma_alloc(uart, &uart->dma_rx, uart->rx_mode, uart->rx_vfifo);
- if (!uart->rx_vfifo || ret) {
- uart->rx_mode = UART_NON_DMA;
- MSG(ERR, "RX DMA alloc fail [%d]\n", ret);
- }
- }
- if (uart->tx_mode == UART_TX_VFIFO_DMA) {
- uart->tx_vfifo = mtk_uart_vfifo_alloc(uart, UART_TX_VFIFO);
- ret = mtk_uart_dma_alloc(uart, &uart->dma_tx, uart->tx_mode, uart->tx_vfifo);
- if (!uart->tx_vfifo || ret) {
- uart->tx_mode = UART_NON_DMA;
- MSG(ERR, "TX DMA alloc fail [%d]\n", ret);
- }
- }
- /* start vfifo dma */
- if (uart->tx_mode == UART_TX_VFIFO_DMA) {
- uart->write_allow = mtk_uart_vfifo_write_allow;
- uart->write_byte = mtk_uart_vfifo_write_byte;
- mtk_uart_vfifo_enable(uart, uart->tx_vfifo);
- mtk_uart_dma_setup(uart, &uart->dma_tx);
- if (mtk_uart_dma_start(uart, &uart->dma_tx))
- MSG(ERR, "mtk_uart_dma_start fails\n");
- hrtimer_init(&uart->tx_vfifo->flush, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
- uart->tx_vfifo->flush.function = mtk_uart_tx_vfifo_timeout;
- } else if (uart->tx_mode == UART_NON_DMA) {
- uart->write_allow = mtk_uart_write_allow;
- uart->write_byte = mtk_uart_write_byte;
- }
- if (uart->rx_mode == UART_RX_VFIFO_DMA) {
- uart->read_allow = mtk_uart_vfifo_read_allow;
- uart->read_byte = mtk_uart_vfifo_read_byte;
- mtk_uart_vfifo_enable(uart, uart->rx_vfifo);
- mtk_uart_dma_setup(uart, &uart->dma_rx);
- if (mtk_uart_dma_start(uart, &uart->dma_rx))
- MSG(ERR, "mtk_uart_dma_start fails\n");
- } else if (uart->rx_mode == UART_NON_DMA) {
- uart->read_allow = mtk_uart_read_allow;
- uart->read_byte = mtk_uart_read_byte;
- }
- #endif
- if (uart->tx_mode == UART_TX_VFIFO_DMA || uart->rx_mode == UART_RX_VFIFO_DMA) {
- if (!bt_port || (bt_port && (uart != bt_port)))
- mtk_uart_disable_dpidle(uart);
- } else if (uart->tx_mode == UART_NON_DMA && uart->rx_mode == UART_NON_DMA) {
- mtk_uart_enable_dpidle(uart);
- }
- uart->tx_stop = 0;
- uart->rx_stop = 0;
- /* After applying UART as Level-Triggered IRQ, the function must be called or
- * the interrupt will be incorrect activated.
- */
- mtk_uart_fifo_set_trig(uart, uart->tx_trig, uart->rx_trig);
- mtk_uart_enable_sleep(uart);
- /* enable interrupts */
- mtk_uart_enable_intrs(uart, mask);
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- /* disable the port, disable any break condition that may be in effect, and
- * free any interrupt resources
- */
- static void mtk_uart_shutdown(struct uart_port *port)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- MSG_FUNC_ENTRY();
- /* disable interrupts */
- mtk_uart_disable_intrs(uart, UART_IER_ALL_INTS);
- #if defined(ENABLE_VFIFO)
- /* free dma channels and vfifo */
- mtk_uart_dma_free(uart, &uart->dma_tx);
- mtk_uart_dma_free(uart, &uart->dma_rx);
- #endif
- mdelay(1);
- mtk_uart_fifo_flush(uart);
- /* release irq line */
- free_irq(port->irq, port);
- mtk_uart_enable_dpidle(uart);
- /* the uart port will be powered off in power_mgnt */
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_flush_buffer(struct uart_port *port)
- {
- #if defined(ENABLE_DEBUG)
- struct mtk_uart *uart = (struct mtk_uart *)port;
- MSG_FUNC_ENTRY();
- #endif
- /* mtk_uart_fifo_flush(uart); */
- }
- /*---------------------------------------------------------------------------*/
- /*
- * For stability test
- */
- void mtk_uart_update_sysclk(void)
- {
- int i;
- unsigned long flags;
- struct mtk_uart *uart;
- struct uart_port *port;
- unsigned int baud;
- for (i = 0; i < UART_NR; i++) {
- uart = &mtk_uarts[i];
- port = &uart->port;
- baud = uart->baudrate;
- port->uartclk = UART_SYSCLK; /* mt6575_get_bus_freq()*1000/4; */
- uart->sysclk = UART_SYSCLK; /* mt6575_get_bus_freq()*1000/4; */
- if (baud == 0)
- continue; /* The istance is not initialized yet. */
- spin_lock_irqsave(&port->lock, flags);
- mtk_uart_set_baud(uart, baud);
- spin_unlock_irqrestore(&port->lock, flags);
- }
- }
- EXPORT_SYMBOL(mtk_uart_update_sysclk);
- /*---------------------------------------------------------------------------*/
- /* change the port parameters, including word length, parity, stop bits.
- * update read_status_mask and ignore_status_mask to indicate the types of
- * events we are interrested in receiving
- */
- static void mtk_uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- unsigned long flags;
- unsigned int baud;
- int datalen, mode;
- int parity = 0;
- int stopbit = 1;
- MSG_FUNC_ENTRY();
- /* datalen : default 8bits */
- switch (termios->c_cflag & CSIZE) {
- case CS5:
- datalen = 5;
- break;
- case CS6:
- datalen = 6;
- break;
- case CS7:
- datalen = 7;
- break;
- case CS8:
- default:
- datalen = 8;
- break;
- }
- /* stopbit : default 1 */
- if (termios->c_cflag & CSTOPB)
- stopbit = 2;
- /* parity : default none */
- if (termios->c_cflag & PARENB) {
- if (termios->c_cflag & PARODD)
- parity = 1; /* odd */
- else
- parity = 2; /* even */
- }
- spin_lock_irqsave(&port->lock, flags);
- /* read status mask */
- port->read_status_mask = 0;
- if (termios->c_iflag & INPCK) {
- /* frame error, parity error */
- port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
- }
- if (termios->c_iflag & (BRKINT | PARMRK)) {
- /* break error */
- port->read_status_mask |= UART_LSR_BI;
- }
- port->ignore_status_mask = 0;
- if (termios->c_iflag & IGNPAR) {
- /* ignore parity and framing errors */
- port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
- }
- if (termios->c_iflag & IGNBRK) {
- /* ignore break errors. */
- port->ignore_status_mask |= UART_LSR_BI;
- if (termios->c_iflag & IGNPAR) {
- /* ignore overrun errors */
- port->ignore_status_mask |= UART_LSR_OE;
- }
- }
- /* ignore all characters if CREAD is not set */
- if ((termios->c_cflag & CREAD) == 0)
- uart->ignore_rx = 1;
- /* update per port timeout */
- baud = uart_get_baud_rate(port, termios, old, 0, uart->sysclk); /*when dividor is 1, baudrate = clock */
- uart_update_timeout(port, termios->c_cflag, baud);
- mtk_uart_config(uart, datalen, stopbit, parity);
- mtk_uart_set_baud(uart, baud);
- /* setup fifo trigger level */
- mtk_uart_fifo_set_trig(uart, uart->tx_trig, uart->rx_trig);
- /* setup hw flow control: only port 0 ~1 support hw rts/cts */
- MSG(CFG, "c_lflag:%X, c_iflag:%X, c_oflag:%X, c_cflag:%X\n", termios->c_lflag, termios->c_iflag,
- termios->c_oflag, termios->c_cflag);
- if (HW_FLOW_CTRL_PORT(uart) && (termios->c_cflag & CRTSCTS) && (!(termios->c_iflag & 0x80000000))) {
- pr_debug("Hardware Flow Control\n");
- mode = UART_FC_HW;
- } else if (termios->c_iflag & 0x80000000) {
- pr_debug("MTK Software Flow Control\n");
- mode = UART_FC_SW;
- } else if (termios->c_iflag & (IXON | IXOFF | IXANY)) {
- pr_debug("Linux default SW Flow Control\n");
- mode = UART_FC_NONE;
- } else {
- pr_debug("No Flow Control\n");
- mode = UART_FC_NONE;
- }
- mtk_uart_set_flow_ctrl(uart, mode);
- /* determine if port should enable modem status interrupt */
- if (UART_ENABLE_MS(port, termios->c_cflag))
- uart->ms_enable = 1;
- else
- uart->ms_enable = 0;
- if (console_port && (uart == console_port) && uart->port.cons)
- uart->port.cons->cflag = termios->c_cflag;
- spin_unlock_irqrestore(&port->lock, flags);
- }
- /*---------------------------------------------------------------------------*/
- /* perform any power management related activities on the port */
- static void mtk_uart_power_mgnt(struct uart_port *port, unsigned int state, unsigned int oldstate)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- MSG(FUC, "%s(%d->%d)\n", __func__, oldstate, state);
- switch (state) {
- case 0:
- mtk_uart_power_up(uart);
- break;
- case 3:
- mtk_uart_power_down(uart);
- break;
- default:
- MSG(ERR, "unknown pm: %d\n", state);
- }
- }
- /*---------------------------------------------------------------------------*/
- /* return a pointer to a string constant describing the port */
- static const char *mtk_uart_type(struct uart_port *port)
- {
- return "MTK UART";
- }
- /*---------------------------------------------------------------------------*/
- /* release any memory and io region resources currently in used by the port */
- static void mtk_uart_release_port(struct uart_port *port)
- {
- }
- /*---------------------------------------------------------------------------*/
- /* request any memory and io region resources required by the port */
- static int mtk_uart_request_port(struct uart_port *port)
- {
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- /* perform any autoconfiguration steps required by the port.
- * it's expected to claim the resources and map the port.
- */
- static void mtk_uart_config_port(struct uart_port *port, int flags)
- {
- struct mtk_uart *uart = (struct mtk_uart *)port;
- if (flags & UART_CONFIG_TYPE) {
- if (mtk_uart_request_port(port))
- MSG(ERR, "mtk_uart_request_port fail\n");
- port->type = PORT_MTK;
- }
- }
- /*---------------------------------------------------------------------------*/
- /* verify if the new serial information contained within 'ser' is suitable */
- static int mtk_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
- {
- #if (defined(ENABLE_DEBUG) || defined(SERIAL_STRUCT_EXT)) /*[ALPS00142658] Fix unused variable waring */
- struct mtk_uart *uart = (struct mtk_uart *)port;
- #endif
- int ret = 0;
- MSG(FUC, "%s: %8x, %d, %d\n", __func__, ser->flags, ser->custom_divisor, uart->custom_baud);
- if (ser->type != PORT_UNKNOWN && ser->type != PORT_MTK)
- ret = -EINVAL;
- if (ser->irq != port->irq)
- ret = -EINVAL;
- if (ser->baud_base < 110)
- ret = -EINVAL;
- #if defined(SERIAL_STRUCT_EXT)
- /*EXtension: the custom baudrate is stored in reserved field */
- uart->custom_baud = ser->reserved[0];
- #endif
- return ret;
- }
- /*---------------------------------------------------------------------------*/
- /* perform any port specific IOCTLs */
- static int mtk_uart_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
- {
- #if defined(ENABLE_DEBUG)
- struct mtk_uart *uart = (struct mtk_uart *)port;
- MSG(FUC, "IOCTL: %8X\n", cmd);
- #endif
- return -ENOIOCTLCMD;
- }
- /*---------------------------------------------------------------------------*/
- #ifdef CONFIG_CONSOLE_POLL
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_get_poll_char(struct uart_port *port)
- { /* don't care vfifo setting */
- struct mtk_uart *uart = (struct mtk_uart *)port;
- /* [ALPS00033048] For Linux 2.6.35 kgdb chagne, using while loop may block kgdb,
- * return NO_POLL_CHAR directly if no data to read */
- #if 0
- while (!(uart->read_status(uart) & UART_LSR_DR))
- cpu_relax();
- #else
- if (!mtk_uart_data_ready(uart))
- return NO_POLL_CHAR;
- #endif
- /* End of [ALPS00033048] */
- return mtk_uart_read_byte(uart);
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_put_poll_char(struct uart_port *port, unsigned char c)
- { /* don't care vfifo setting */
- struct mtk_uart *uart = (struct mtk_uart *)port;
- while (!mtk_uart_write_allow(uart))
- barrier();
- mtk_uart_write_byte(uart, c);
- }
- /*---------------------------------------------------------------------------*/
- #endif
- /*---------------------------------------------------------------------------*/
- static struct uart_ops mtk_uart_ops = {
- .tx_empty = mtk_uart_tx_empty,
- .set_mctrl = mtk_uart_set_mctrl,
- .get_mctrl = mtk_uart_get_mctrl,
- .stop_tx = mtk_uart_stop_tx,
- .start_tx = mtk_uart_start_tx,
- .stop_rx = mtk_uart_stop_rx,
- .send_xchar = mtk_uart_send_xchar,
- .enable_ms = mtk_uart_enable_ms,
- .break_ctl = mtk_uart_break_ctl,
- .startup = mtk_uart_startup,
- .shutdown = mtk_uart_shutdown,
- .flush_buffer = mtk_uart_flush_buffer,
- .set_termios = mtk_uart_set_termios,
- .pm = mtk_uart_power_mgnt,
- .type = mtk_uart_type,
- .release_port = mtk_uart_release_port,
- .request_port = mtk_uart_request_port,
- .config_port = mtk_uart_config_port,
- .verify_port = mtk_uart_verify_port,
- .ioctl = mtk_uart_ioctl,
- #ifdef CONFIG_CONSOLE_POLL
- .poll_get_char = mtk_uart_get_poll_char,
- .poll_put_char = mtk_uart_put_poll_char,
- #endif
- };
- /*---------------------------------------------------------------------------*/
- static struct uart_driver mtk_uart_drv = {
- .owner = THIS_MODULE,
- .driver_name = DRV_NAME,
- .dev_name = "ttyMT",
- .major = UART_MAJOR,
- .minor = UART_MINOR,
- .nr = UART_NR,
- #if defined(CONFIG_MTK_SERIAL_CONSOLE) && !defined(CONFIG_MTK_SERIAL_MODEM_TEST)
- .cons = &mtk_uart_console,
- #endif
- };
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_probe(struct platform_device *pdev)
- {
- struct mtk_uart *uart;
- int err;
- #if !defined(CONFIG_MTK_FPGA)
- #if !defined(CONFIG_MTK_CLKMGR)
- static const char * const clk_uart_name[] = {
- "uart0-main",
- "uart1-main",
- "uart2-main",
- "uart3-main",
- "uart4-main",
- };
- struct mtk_uart_setting *uart_setting = NULL;
- #endif
- #if !defined(CONFIG_MTK_LEGACY)
- /* for GPIO pinctrl */
- struct pinctrl *ppinctrl = NULL;
- #endif
- #endif /* !defined(CONFIG_MTK_FPGA) */
- #ifdef CONFIG_OF
- if (pdev->dev.of_node) {
- struct device_node *node = pdev->dev.of_node;
- err = of_property_read_u32(node, "cell-index", &pdev->id);
- if (err)
- pr_err("[DTS] get uart platform_device id fail!!\n");
- }
- if (pdev->id >= UART_NR) {
- pr_err("DTS cell ID %d > UART nuber %d\n", pdev->id, UART_NR);
- return -ENODEV;
- }
- #endif
- uart = &mtk_uarts[pdev->id];
- MSG_FUNC_ENTRY();
- /* For clock setting */
- #if !defined(CONFIG_MTK_CLKMGR) && !defined(CONFIG_MTK_FPGA)
- uart_setting = get_uart_default_settings(pdev->id);
- uart_setting->clk_uart_main = devm_clk_get(&pdev->dev, clk_uart_name[pdev->id]);
- if (IS_ERR(uart_setting->clk_uart_main)) {
- pr_err("[UART%d][CCF]cannot get %s clock. ptr_err:%ld\n", pdev->id, clk_uart_name[pdev->id]
- , PTR_ERR(uart_setting->clk_uart_main));
- return PTR_ERR(uart_setting->clk_uart_main);
- }
- pr_debug("[UART%d][CCF]clk_uart%d_main:%p\n", pdev->id, pdev->id, uart_setting->clk_uart_main);
- if (pdev->id == 0) {
- struct clk *clk_uart0_dma = devm_clk_get(&pdev->dev, "uart-apdma");
- if (IS_ERR(clk_uart0_dma)) {
- pr_err("[UART][CCF]cannot get clk_uart0_dma clock. ptr_err:%ld\n", PTR_ERR(clk_uart0_dma));
- return PTR_ERR(clk_uart0_dma);
- }
- set_uart_dma_clk(pdev->id, clk_uart0_dma);
- pr_debug("[UART][CCF]clk_uart0_dma:%p\n", clk_uart0_dma);
- }
- #else /* !defined(CONFIG_MTK_CLKMGR) && !defined(CONFIG_MTK_FPGA) */
- pr_debug("[UART][CCF]mtk_uart_probe CONFIG_MTK_CLKMGR or CONFIG_MTK_FPGA is defined!\n");
- #endif /*!defined(CONFIG_MTK_CLKMGR) && !defined(CONFIG_MTK_FPGA) */
- /* For GPIO setting */
- #if !defined(CONFIG_MTK_LEGACY) && !defined(CONFIG_MTK_FPGA)
- ppinctrl = devm_pinctrl_get(&pdev->dev);
- if (IS_ERR(ppinctrl)) {
- err = PTR_ERR(ppinctrl);
- pr_err("[UART%d][PinC]cannot find pinctrl. ptr_err:%ld\n", pdev->id, PTR_ERR(ppinctrl));
- return err;
- }
- set_uart_pinctrl(pdev->id, ppinctrl);
- pr_debug("[UART%d][PinC]set idx:%d, ppinctrl:%p\n", pdev->id, pdev->id, ppinctrl);
- #else /* !defined(CONFIG_MTK_LEGACY) && !defined(CONFIG_MTK_FPGA) */
- pr_debug("[UART][PinC]mtk_uart_probe CONFIG_MTK_LEGACY or CONFIG_MTK_FPGA is defined!\n");
- #endif /* !defined(CONFIG_MTK_LEGACY) && !defined(CONFIG_MTK_FPGA) */
- if (mtk_uart_plat_info_query("ADD_DMA_BIT_MASK_32"))
- pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
- uart->port.dev = &pdev->dev;
- err = uart_add_one_port(&mtk_uart_drv, &uart->port);
- if (!err)
- platform_set_drvdata(pdev, uart);
- #if defined(ENABLE_VFIFO)
- err = mtk_uart_vfifo_create(uart);
- if (err) {
- mtk_uart_vfifo_delete(uart);
- DEV_ERR("create vff buffer fail:%d\n", err);
- }
- #endif
- return err;
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_remove(struct platform_device *pdev)
- {
- struct mtk_uart *uart = platform_get_drvdata(pdev);
- int err;
- platform_set_drvdata(pdev, NULL);
- if (!uart)
- return -EINVAL;
- err = uart_remove_one_port(&mtk_uart_drv, &uart->port);
- #if defined(ENABLE_VFIFO)
- mtk_uart_vfifo_delete(uart);
- #endif
- return err;
- }
- /*---------------------------------------------------------------------------*/
- #ifdef CONFIG_PM
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_syscore_suspend(void)
- {
- int ret = 0;
- unsigned long flags;
- if (bt_port) {
- struct mtk_uart *uart = bt_port;
- spin_lock_irqsave(&mtk_uart_bt_lock, flags);
- ret = uart_suspend_port(&mtk_uart_drv, &uart->port);
- /* To keeping uart idle state */
- /* tx pin: idle->high power down->low */
- mtk_uart_switch_tx_to_gpio(uart);
- spin_unlock_irqrestore(&mtk_uart_bt_lock, flags);
- }
- return ret;
- }
- /*---------------------------------------------------------------------------*/
- static void mtk_uart_syscore_resume(void)
- {
- int ret = 0;
- unsigned long flags;
- if (bt_port) {
- struct mtk_uart *uart = bt_port;
- spin_lock_irqsave(&mtk_uart_bt_lock, flags);
- mtk_uart_switch_to_tx(uart);
- ret = uart_resume_port(&mtk_uart_drv, &uart->port);
- spin_unlock_irqrestore(&mtk_uart_bt_lock, flags);
- disable_irq(uart->port.irq);
- }
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_suspend(struct platform_device *pdev, pm_message_t state)
- {
- int ret = 0;
- struct mtk_uart *uart = platform_get_drvdata(pdev);
- /* For console_suspend_enabled=0 */
- if (console_suspend_enabled == 0 && uart == console_port && uart->poweron_count > 0)
- mtk_uart_save(uart);
- if (uart && (uart->nport < UART_NR) && (uart != bt_port)) {
- ret = uart_suspend_port(&mtk_uart_drv, &uart->port);
- mtk_uart_switch_rx_to_gpio(uart);
- }
- return ret;
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_resume(struct platform_device *pdev)
- {
- int ret = 0;
- struct mtk_uart *uart = platform_get_drvdata(pdev);
- if (uart && (uart->nport < UART_NR) && (uart != bt_port)) {
- mtk_uart_switch_to_rx(uart);
- ret = uart_resume_port(&mtk_uart_drv, &uart->port);
- }
- return ret;
- }
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_pm_suspend(struct device *device)
- {
- struct platform_device *pdev;
- /*pr_debug("calling %s()\n", __func__);*/
- pdev = to_platform_device(device);
- BUG_ON(pdev == NULL);
- return mtk_uart_suspend(pdev, PMSG_SUSPEND);
- }
- static int mtk_uart_pm_resume(struct device *device)
- {
- struct platform_device *pdev;
- /*pr_debug("calling %s()\n", __func__);*/
- pdev = to_platform_device(device);
- BUG_ON(pdev == NULL);
- return mtk_uart_resume(pdev);
- }
- static int mtk_uart_pm_freeze(struct device *device)
- {
- struct platform_device *pdev;
- struct mtk_uart *uart = NULL;
- int ret = 0;
- int port_idx = 0;
- pdev = to_platform_device(device);
- BUG_ON(pdev == NULL);
- uart = platform_get_drvdata(pdev);
- port_idx = uart->nport;
- if (uart_freeze_enable[port_idx]) {
- ret = uart_suspend_port(&mtk_uart_drv, &uart->port);
- pr_warn("[%s] here.\n", __func__);
- /* To keeping uart idle state */
- /* tx pin: idle->high power down->low */
- mtk_uart_switch_tx_to_gpio(uart);
- pr_warn("[%s] done.\n", __func__);
- }
- return 0; /* mtk_uart_suspend(pdev, PMSG_SUSPEND); */
- }
- static int mtk_uart_pm_restore(struct device *device)
- {
- struct platform_device *pdev;
- struct mtk_uart *uart;
- int ret = 0;
- int port_idx = 0;
- pdev = to_platform_device(device);
- BUG_ON(pdev == NULL);
- uart = platform_get_drvdata(pdev);
- port_idx = uart->nport;
- if (uart_freeze_enable[port_idx]) {
- mtk_uart_switch_to_tx(uart);
- ret = uart_resume_port(&mtk_uart_drv, &uart->port);
- pr_warn("[%s] uart (%p) base: 0x%lx, nport %d, dma mode: %d\n", __func__,
- uart, uart->base, uart->nport, uart->dma_mode);
- }
- return mtk_uart_resume(pdev);
- }
- static int mtk_uart_pm_restore_noirq(struct device *device)
- {
- /* FIXME. not get GIC_DIST_BASE from DTS */
- #ifndef CONFIG_OF
- unsigned int gic_pending;
- #endif
- struct mtk_uart *uart;
- /* pr_warn("calling %s()\n", __func__); */
- uart = dev_get_drvdata(device);
- if (!uart || !uart->setting) {
- pr_warn("[%s] uart or uart->setting is null!!\n", __func__);
- return 0;
- }
- mtk_uart_fifo_set_trig(uart, uart->tx_trig, uart->rx_trig);
- #ifdef CONFIG_OF
- irq_set_irq_type(uart->setting->irq_num, uart->setting->irq_flags);
- #else
- if (uart->setting->irq_sen == MT_EDGE_SENSITIVE)
- irq_set_irq_type(uart->setting->irq_num, IRQF_TRIGGER_FALLING);
- else
- irq_set_irq_type(uart->setting->irq_num, IRQF_LEVEL_TRIGGER_POLARITY);
- #endif
- #define GIC_DIST_PENDING_SET 0x200
- if (uart->tx_vfifo && uart->tx_mode == UART_TX_VFIFO_DMA) {
- irq_set_irq_type(uart->tx_vfifo->irq_id, IRQF_LEVEL_TRIGGER_POLARITY);
- /* FIXME. not get GIC_DIST_BASE from DTS */
- #ifndef CONFIG_OF
- gic_pending = DRV_Reg32(GIC_DIST_BASE + GIC_DIST_PENDING_SET + uart->tx_vfifo->irq_id / 32 * 4);
- pr_warn("[%s] tx_vfifo(%p) gic_pending_mask(0x%08x)\n", __func__, uart->tx_vfifo->base, gic_pending);
- #endif
- }
- if (uart->rx_vfifo && uart->rx_mode == UART_RX_VFIFO_DMA) {
- irq_set_irq_type(uart->rx_vfifo->irq_id, IRQF_LEVEL_TRIGGER_POLARITY);
- /* FIXME. not get GIC_DIST_BASE from DTS */
- #ifndef CONFIG_OF
- gic_pending = DRV_Reg32(GIC_DIST_BASE + GIC_DIST_PENDING_SET + uart->rx_vfifo->irq_id / 32 * 4);
- pr_warn("[%s] rx_vfifo(%p) gic_pending_mask(0x%08x)\n", __func__, uart->rx_vfifo->base, gic_pending);
- #endif
- }
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- #else /*CONFIG_PM */
- /*---------------------------------------------------------------------------*/
- #define mtk_uart_pm_suspend NULL
- #define mtk_uart_pm_resume NULL
- #define mtk_uart_pm_freeze NULL
- #define mtk_uart_pm_restore NULL
- #define mtk_uart_pm_restore_noirq NULL
- /*---------------------------------------------------------------------------*/
- #endif /*CONFIG_PM */
- /*---------------------------------------------------------------------------*/
- const struct dev_pm_ops mtk_uart_pm_ops = {
- .suspend = mtk_uart_pm_suspend,
- .resume = mtk_uart_pm_resume,
- .freeze = mtk_uart_pm_freeze,
- .thaw = mtk_uart_pm_restore,
- .poweroff = mtk_uart_pm_suspend,
- .restore = mtk_uart_pm_restore,
- .restore_noirq = mtk_uart_pm_restore_noirq,
- };
- /*---------------------------------------------------------------------------*/
- static int mtk_uart_init_ports(void)
- {
- int i;
- #ifdef CONFIG_OF
- #if defined(ENABLE_VFIFO)
- int idx;
- struct mtk_uart_vfifo *vfifo;
- #endif
- void __iomem *apdma_uart0_base = 0;
- #endif
- struct mtk_uart *uart;
- unsigned long base;
- spin_lock_init(&mtk_console_lock);
- #ifdef CONFIG_OF
- apdma_uart0_base = get_apdma_uart0_base();
- #endif
- for (i = 0; i < UART_NR; i++) {
- #ifdef CONFIG_OF
- set_uart_default_settings(i);
- #endif
- uart = &mtk_uarts[i];
- uart->setting = get_uart_default_settings(i);
- #ifdef CONFIG_OF
- #if defined(ENABLE_VFIFO)
- if (uart->setting->vff) {
- if (i * 2 < sizeof(mtk_uart_vfifo_port) / sizeof(mtk_uart_vfifo_port[0])) {
- for (idx = i * 2; idx < i * 2 + 2; idx++) {
- vfifo = &mtk_uart_vfifo_port[idx];
- vfifo->base = (apdma_uart0_base + 0x0080 * idx);
- vfifo->irq_id = get_uart_vfifo_irq_id(idx);
- }
- }
- }
- #endif
- #endif
- base = uart->setting->uart_base;
- uart->port.iotype = UPIO_MEM;
- #ifdef CONFIG_OF
- uart->port.mapbase = uart->setting->uart_phys_base; /* for ioremap */
- #else
- uart->port.mapbase = IO_VIRT_TO_PHYS(base); /* for ioremap */
- #endif
- uart->port.membase = (unsigned char __iomem *)base;
- uart->port.irq = uart->setting->irq_num;
- uart->port.fifosize = UART_FIFO_SIZE;
- uart->port.ops = &mtk_uart_ops;
- uart->port.flags = UPF_BOOT_AUTOCONF;
- uart->port.line = i;
- uart->port.uartclk = UART_SYSCLK;
- /* pll_for_uart = mt6575_get_bus_freq(); */
- /* uart->port.uartclk = mt6575_get_bus_freq()*1000/4; */
- spin_lock_init(&uart->port.lock);
- uart->base = base;
- uart->auto_baud = CFG_UART_AUTOBAUD;
- uart->nport = i;
- uart->sysclk = UART_SYSCLK; /* FIXME */
- /* uart->sysclk = mt6575_get_bus_freq()*1000/4; */
- uart->dma_mode = uart->setting->dma_mode;
- uart->tx_mode = uart->setting->tx_mode;
- uart->rx_mode = uart->setting->rx_mode;
- uart->tx_trig = uart->setting->tx_trig;
- uart->rx_trig = uart->setting->rx_trig;
- uart->write_allow = mtk_uart_write_allow;
- uart->read_allow = mtk_uart_read_allow;
- uart->write_byte = mtk_uart_write_byte;
- uart->read_byte = mtk_uart_read_byte;
- uart->read_status = mtk_uart_read_status;
- uart->poweron_count = 0;
- uart->timeout_count = 0;
- uart->baudrate = 0;
- uart->custom_baud = 0;
- uart->registers.dll = 1;
- uart->registers.dlh = 0;
- uart->registers.ier = 0;
- uart->registers.lcr = 0;
- uart->registers.mcr = 0;
- uart->registers.fcr = 0;
- uart->registers.lsr = 0x60;
- uart->registers.efr = 0;
- uart->registers.highspeed = 0;
- uart->registers.sample_count = 0;
- uart->registers.sample_point = 0xff;
- uart->registers.fracdiv_l = 0;
- uart->registers.fracdiv_m = 0;
- uart->registers.escape_en = 0;
- uart->registers.guard = 0;
- uart->registers.rx_sel = 0;
- uart->evt_mask = (unsigned int)get_uart_evt_mask(i);
- #if defined(CONFIG_MTK_SERIAL_MODEM_TEST)
- if (get_modem_uart(i)) {
- /* u32 dat = UART_READ32(HW_MISC); // mtk does NOT has this register */
- mtk_uart_power_up(uart); /* power up */
- /* reg_sync_writel(dat | mask[i], HW_MISC); */
- continue;
- }
- #else
- /* mtk_uart_power_up(uart); */
- mtk_uart_disable_intrs(uart, UART_IER_ALL_INTS);
- #ifdef CONFIG_OF
- irq_set_irq_type(uart->setting->irq_num, uart->setting->irq_flags);
- #else
- if (uart->setting->irq_sen == MT_EDGE_SENSITIVE)
- irq_set_irq_type(uart->setting->irq_num, IRQF_EDGE_TRIGGER_POLARITY);
- else
- irq_set_irq_type(uart->setting->irq_num, IRQF_LEVEL_TRIGGER_POLARITY);
- #endif
- mtk_uart_fifo_init(uart);
- mtk_uart_set_mode(uart, uart->dma_mode);
- /* mtk_uart_power_down(uart); */
- #endif
- }
- #if defined(CONFIG_MTK_SERIAL_MODEM_TEST)
- /*NOTICE: for enabling modem test, UART4 needs to be disabled. Howerver, if CONFIG_MTK_SERIAL_CONSOLE
- is defined, resume will fail. Since the root cause is not clear, only disable the console-related
- function. */
- /*printk("HW_MISC: 0x%08X\n", UART_READ32(HW_MISC)); */ /* mtk does NOT has this register */
- #endif
- return 0;
- }
- /*---------------------------------------------------------------------------*/
- #ifdef CONFIG_OF
- static const struct of_device_id apuart_of_ids[] = {
- {.compatible = "mediatek,AP_UART0",},
- {.compatible = "mediatek,AP_UART1",},
- #ifndef CONFIG_MTK_FPGA
- {.compatible = "mediatek,AP_UART2",},
- {.compatible = "mediatek,AP_UART3",},
- #if (UART_NR > 4)
- {.compatible = "mediatek,AP_UART4",},
- #endif
- #endif
- {.compatible = "mediatek,mt6735-uart",},
- {.compatible = "mediatek,mt6755-uart",},
- {.compatible = "mediatek,mt8173-uart",},
- {.compatible = "mediatek,mt6797-uart",},
- {.compatible = "mediatek,mt8163-uart",},
- {}
- };
- #endif
- static struct platform_driver mtk_uart_dev_drv = {
- .probe = mtk_uart_probe,
- .remove = mtk_uart_remove,
- #ifdef CONFIG_PM
- .suspend = mtk_uart_suspend,
- .resume = mtk_uart_resume,
- #endif
- .driver = {
- .name = DRV_NAME,
- .owner = THIS_MODULE,
- #ifdef CONFIG_OF
- .of_match_table = apuart_of_ids,
- #endif
- #ifdef CONFIG_PM
- .pm = &mtk_uart_pm_ops,
- #endif
- }
- };
- #ifdef CONFIG_PM
- static struct syscore_ops mtk_uart_syscore_ops = {
- .suspend = mtk_uart_syscore_suspend,
- .resume = mtk_uart_syscore_resume,
- };
- static int __init mtk_uart_init_ops(void)
- {
- register_syscore_ops(&mtk_uart_syscore_ops);
- return 0;
- }
- #endif
- /*---------------------------------------------------------------------------*/
- static int __init mtk_uart_init(void)
- {
- int ret = 0;
- tx_history.buffer = kzalloc(UART_HISTORY_DATA_SIZE, GFP_KERNEL);
- rx_history.buffer = kzalloc(UART_HISTORY_DATA_SIZE, GFP_KERNEL);
- tx_history.index = -1;
- rx_history.index = -1;
- if (!tx_history.buffer || !rx_history.buffer)
- return -ENOMEM;
- #ifndef CONFIG_MTK_SERIAL_CONSOLE
- mtk_uart_init_ports();
- #endif
- #if defined(ENABLE_SYSFS)
- mtk_uart_sysfs();
- #endif
- ret = uart_register_driver(&mtk_uart_drv);
- if (ret)
- return ret;
- ret = platform_driver_register(&mtk_uart_dev_drv);
- if (ret) {
- uart_unregister_driver(&mtk_uart_drv);
- return ret;
- }
- #ifdef CONFIG_PM
- mtk_uart_init_ops();
- #endif
- #ifdef ENABLE_RAW_DATA_DUMP
- mtk_uart_init_debug_spinlock();
- #endif
- spin_lock_init(&mtk_uart_bt_lock);
- return ret;
- }
- /*---------------------------------------------------------------------------*/
- static void __exit mtk_uart_exit(void)
- {
- platform_driver_unregister(&mtk_uart_dev_drv);
- uart_unregister_driver(&mtk_uart_drv);
- }
- /*---------------------------------------------------------------------------*/
- #ifdef ENABLE_UART_SLEEP
- int request_uart_to_sleep(void)
- {
- u32 val1;
- int i = 0;
- int uart_idx = 0;
- struct mtk_uart *uart;
- unsigned long base;
- for (uart_idx = 0; uart_idx < UART_NR; uart_idx++) {
- /*
- #if defined(CONFIG_MTK_HDMI_SUPPORT)
- #ifdef MHL_UART_SHARE_PIN
- {
- for K2 uart2 and mhl share pin,
- if mhl is in low power mode, uart rx is not working, so bypass it.
- if ((is_hdmi_active() == 0) && (uart_idx == 1))
- continue;
- }
- #endif
- #endif
- */
- uart = &mtk_uarts[uart_idx];
- base = uart->base;
- if (uart->poweron_count > 0) {
- /* request UART to sleep */
- val1 = UART_READ32(UART_SLEEP_REQ);
- reg_sync_writel(val1 | UART_CLK_OFF_REQ, UART_SLEEP_REQ);
- /* wait for UART to ACK */
- while (!(UART_READ32(UART_SLEEP_ACK) & UART_CLK_OFF_ACK)) {
- if (i++ >= WAIT_UART_ACK_TIMES) {
- reg_sync_writel(val1, UART_SLEEP_REQ);
- pr_err_ratelimited("[UART]CANNOT GET UART[%d] SLEEP ACK\n",
- uart_idx);
- /* dump_uart_reg(); */
- return -EBUSY;
- }
- udelay(10);
- }
- } else {
- /* printk("[UART%d] clock is off\n", uart->nport); */
- }
- }
- return 0;
- }
- EXPORT_SYMBOL(request_uart_to_sleep);
- int request_uart_to_wakeup(void)
- {
- /* printk(KERN_ERR "Request UART sleep\n"); */
- u32 val1;
- int i = 0;
- int uart_idx = 0;
- struct mtk_uart *uart;
- unsigned long base;
- for (uart_idx = 0; uart_idx < UART_NR; uart_idx++) {
- uart = &mtk_uarts[uart_idx];
- base = uart->base;
- if (uart->poweron_count > 0) {
- /* wakeup uart */
- val1 = UART_READ32(UART_SLEEP_REQ);
- reg_sync_writel(val1 & (~UART_CLK_OFF_REQ), UART_SLEEP_REQ);
- /* wait for UART to ACK */
- while ((UART_READ32(UART_SLEEP_ACK) & UART_CLK_OFF_ACK)) {
- if (i++ >= WAIT_UART_ACK_TIMES) {
- reg_sync_writel(val1, UART_SLEEP_REQ);
- pr_err("[UART]CANNOT GET UART[%d] WAKE ACK\n", uart_idx);
- /* dump_uart_reg(); */
- return -EBUSY;
- }
- udelay(10);
- }
- } else {
- /* printk("[UART%d] clock is off\n", uart->nport); */
- }
- }
- return 0;
- }
- EXPORT_SYMBOL(request_uart_to_wakeup);
- #endif
- /*---------------------------------------------------------------------------*/
- module_init(mtk_uart_init);
- module_exit(mtk_uart_exit);
- /*---------------------------------------------------------------------------*/
- MODULE_AUTHOR("MTK");
- MODULE_DESCRIPTION("MTK Serial Port Driver");
- MODULE_LICENSE("GPL");
|